Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //UVa 11210
- #include <bits/stdc++.h>
- using namespace std;
- string tiles[] = {"DONG", "NAN", "XI", "BEI", "ZHONG", "FA", "BAI"};
- int arr[38], cpy[38];
- bool ans[38];
- void input(string &s){
- if(s[1] == 'W')++arr[s[0]-'0'];
- else if(s[1] == 'T')++arr[s[0]-'0'+10];
- else if(s[1] == 'S')++arr[s[0]-'0'+20];
- else{
- for(int i = 0; i < 7; ++i){
- if(s == tiles[i]){
- ++arr[i+31];
- break;
- }
- }
- }
- }
- void output(){
- for(int i = 11; i <= 19; ++i){
- if(ans[i])cout << ' ' << i-10 << 'T';
- }
- for(int i = 21; i <= 29; ++i){
- if(ans[i])cout << ' ' << i-20 << 'S';
- }
- for(int i = 1; i <= 9; ++i){
- if(ans[i])cout << ' ' << i << 'W';
- }
- for(int i = 31; i <= 37; ++i){
- if(ans[i])cout << ' ' << tiles[i-31];
- }
- }
- bool dfs(int depth, bool pair){//0 := not used, 1 := used
- if(depth == 38)return 1;
- if(arr[depth] == 0)return dfs(depth+1, pair);
- bool ret = 0;
- if(depth < 30){
- if(arr[depth] >= 1 && arr[depth+1] >= 1 && arr[depth+2] >= 1){
- --arr[depth];
- --arr[depth+1];
- --arr[depth+2];
- ret |= dfs(depth, pair);
- ++arr[depth];
- ++arr[depth+1];
- ++arr[depth+2];
- }
- if(arr[depth] >= 2 && !pair){
- arr[depth] -= 2;
- ret |= dfs(depth, 1);
- arr[depth] += 2;
- }
- if(arr[depth] >= 3){
- arr[depth] -= 3;
- ret |= dfs(depth, pair);
- arr[depth] += 3;
- }
- }else{
- if(arr[depth] % 3 == 1)return 0;
- if(arr[depth] == 2 && !pair)return dfs(depth+1, 1);
- if(arr[depth] == 3)return dfs(depth+1, pair);
- }
- return ret;
- }
- int32_t main(){
- string s;
- int cnt = 0;
- while(cin >> s){
- if(s == "0")break;
- ++cnt;
- input(s);
- if(cnt % 13 == 0){
- memcpy(cpy, arr, sizeof(arr));
- bool ok = 0;
- for(int i = 0; i < 3; ++i){
- for(int j = 0; j <= 9; ++j){
- if(arr[i*10+j] == 4)continue;
- ++arr[i*10+j];
- if(dfs(1, 0)){
- ans[i*10+j] = 1;
- ok = 1;
- }
- --arr[i*10+j];
- }
- }
- for(int i = 31; i <= 37; ++i){
- if(arr[i] == 4)continue;
- ++arr[i];
- if(dfs(1, 0)){
- ans[i] = 1;
- ok = 1;
- }
- --arr[i];
- }
- cout << "Case " << cnt/13 << ":";
- if(ok)output();
- else cout << " Not ready";
- cout << '\n';
- memset(arr, 0, sizeof(arr));
- memset(ans, 0, sizeof(ans));
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement