Chinese Mahjong UVA - 11210

vj 传送门
u
va传送门

Chinese Mahjong UVA - 11210 

 这题的话,乍一看有点难 仔细看会发现就暴力枚举一下就好了 反正也就9*3+7 34张牌

  每张牌最多出现四次, 然后如果加入每张牌以后能变成 2 +3*4 (将+顺子或者刻子) 那么就“听” 这张牌,每张牌枚举的时候需要初始化一下现有的牌, 因为可能之前的牌成功被听  需要更新状态。

  代码如下

 

#include <bits/stdc++.h>

using namespace std;

#define _for(i,a,b) for(int i = (a); i < (b); i++)
#define _rep(i,a,b) for(int i = (a); i <= (b); i++)
#define ll long long
#define all(v) (v).begin(), (v).end()

string majiang[] = { "1T","2T","3T","4T","5T","6T","7T","8T","9T",
                     "1S","2S","3S","4S","5S","6S","7S","8S","9S",
                     "1W","2W","3W","4W","5W","6W","7W","8W","9W",
                     "DONG", "NAN", "XI", "BEI", "ZHONG", "FA", "BAI"};
int ID(string ss) {
    _for(i,0,34) if(majiang[i] == ss) return i;
    return -1;
}
int c[34];
bool dfs(int dep) {
    if(dep == 4) return true;
    _for(i,0,34) 
        if(c[i] > 2) 
        {
             c[i]-=3;
             if(dfs(dep+1)) return true;
             c[i]+=3; 
        }   
    _for(i,0,25) 
        if(i%9<7 and c[i] and c[i+1] and c[i+2])
        {
            c[i]--, c[i+1]--, c[i+2]--;
            if(dfs(dep+1)) return true;
            c[i]++, c[i+1]++, c[i+2]++;
        } 
    return false;
}
bool check() { //将 刻子  顺子
    _for(i,0,34) 
        if(c[i] > 1) 
        {  
            c[i]-=2;
            if(dfs(0)) return true;
            c[i]+=2;
        }
    return false;
}
void uva11210() {
    int kase = 1; string s;
    while(cin >> s and s[0] != '0') {
        cout << "Case " << kase++ << ":";
        int a[13];
        a[0] = ID(s);
        _for(i,0,12) cin >> s, a[i+1] = ID(s); 
        int f = 0;
        _for(i,0,34) {
            memset(c, 0, sizeof c);
            _for(j,0,13) c[a[j]]++;
            if(c[i] > 3) continue;
            c[i]++;
            if(check()) cout << " " << majiang[i], f = 1;
            c[i]--;
        }
        if(!f) cout << " Not ready";
        cout << "\n";
    }
    return;
}
int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    uva11210();
    return 0;
}

 

posted @ 2020-08-29 17:40  163467  阅读(153)  评论(0)    收藏  举报