Advertisement
ccbeginner

UVa Q11210

Feb 16th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. //UVa 11210
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. string tiles[] = {"DONG", "NAN", "XI", "BEI", "ZHONG", "FA", "BAI"};
  6. int arr[38], cpy[38];
  7. bool ans[38];
  8.  
  9. void input(string &s){
  10.     if(s[1] == 'W')++arr[s[0]-'0'];
  11.     else if(s[1] == 'T')++arr[s[0]-'0'+10];
  12.     else if(s[1] == 'S')++arr[s[0]-'0'+20];
  13.     else{
  14.         for(int i = 0; i < 7; ++i){
  15.             if(s == tiles[i]){
  16.                 ++arr[i+31];
  17.                 break;
  18.             }
  19.         }
  20.     }
  21. }
  22.  
  23. void output(){
  24.     for(int i = 11; i <= 19; ++i){
  25.         if(ans[i])cout << ' ' << i-10 << 'T';
  26.     }
  27.     for(int i = 21; i <= 29; ++i){
  28.         if(ans[i])cout << ' ' << i-20 << 'S';
  29.     }
  30.     for(int i = 1; i <= 9; ++i){
  31.         if(ans[i])cout << ' ' << i << 'W';
  32.     }
  33.     for(int i = 31; i <= 37; ++i){
  34.         if(ans[i])cout << ' ' << tiles[i-31];
  35.     }
  36. }
  37.  
  38. bool dfs(int depth, bool pair){//0 := not used, 1 := used
  39.     if(depth == 38)return 1;
  40.     if(arr[depth] == 0)return dfs(depth+1, pair);
  41.     bool ret = 0;
  42.     if(depth < 30){
  43.         if(arr[depth] >= 1 && arr[depth+1] >= 1 && arr[depth+2] >= 1){
  44.             --arr[depth];
  45.             --arr[depth+1];
  46.             --arr[depth+2];
  47.             ret |= dfs(depth, pair);
  48.             ++arr[depth];
  49.             ++arr[depth+1];
  50.             ++arr[depth+2];
  51.         }
  52.         if(arr[depth] >= 2 && !pair){
  53.             arr[depth] -= 2;
  54.             ret |= dfs(depth, 1);
  55.             arr[depth] += 2;
  56.         }
  57.         if(arr[depth] >= 3){
  58.             arr[depth] -= 3;
  59.             ret |= dfs(depth, pair);
  60.             arr[depth] += 3;
  61.         }
  62.     }else{
  63.         if(arr[depth] % 3 == 1)return 0;
  64.         if(arr[depth] == 2 && !pair)return dfs(depth+1, 1);
  65.         if(arr[depth] == 3)return dfs(depth+1, pair);
  66.     }
  67.     return ret;
  68. }
  69.  
  70. int32_t main(){
  71.     string s;
  72.     int cnt = 0;
  73.     while(cin >> s){
  74.         if(s == "0")break;
  75.         ++cnt;
  76.         input(s);
  77.         if(cnt % 13 == 0){
  78.             memcpy(cpy, arr, sizeof(arr));
  79.             bool ok = 0;
  80.             for(int i = 0; i < 3; ++i){
  81.                 for(int j = 0; j <= 9; ++j){
  82.                     if(arr[i*10+j] == 4)continue;
  83.                     ++arr[i*10+j];
  84.                     if(dfs(1, 0)){
  85.                         ans[i*10+j] = 1;
  86.                         ok = 1;
  87.                     }
  88.                     --arr[i*10+j];
  89.                 }
  90.             }
  91.             for(int i = 31; i <= 37; ++i){
  92.                 if(arr[i] == 4)continue;
  93.                 ++arr[i];
  94.                 if(dfs(1, 0)){
  95.                     ans[i] = 1;
  96.                     ok = 1;
  97.                 }
  98.                 --arr[i];
  99.             }
  100.            
  101.             cout << "Case " << cnt/13 << ":";
  102.             if(ok)output();
  103.             else cout << " Not ready";
  104.             cout << '\n';
  105.            
  106.             memset(arr, 0, sizeof(arr));
  107.             memset(ans, 0, sizeof(ans));
  108.         }
  109.     }
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement