Chinese Mahjong UVA - 11210 (DFS)

先记录下每一种麻将出现的次数,然后枚举每一种可能得到的麻将,对于这个新的麻将牌,去判断可不可能胡,如果可以胡,就可以把这张牌输出出来。

因为eye只能有一张,所以这个是最好枚举的,就枚举每张牌成为eye的可能,然后对于剩下的牌去判断成为pong和chow的可能,然后判断可不可能胡牌

#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lowbit(x) (x & (-x))
#define INOPEM freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const double pi = 4.0*atan(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 2e6+;
const int maxm = ;
const int mod = 1e9+;
using namespace std; int n, m;
int T, tol;
char *Mahjong[] = {
"", "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 cnt[];
int id[]; void init() {
memset(cnt, , sizeof cnt);
} int find(char *s) {
for(int i=; i<; i++)
if(strcmp(s, Mahjong[i]) == )
return i;
} bool dfs(int count) {
if(count == ) return true;
for(int i=; i<=; i++) {
if(cnt[i] >= ) {
cnt[i] -= ;
if(dfs(count+)) return true;
cnt[i] += ;
}
}
for(int i=; i<=; i++) {
if(cnt[i] >= && cnt[i+] >= && cnt[i+] >= ) {
cnt[i] -= , cnt[i+] -= , cnt[i+] -= ;
if(dfs(count+)) return true;
cnt[i] += , cnt[i+] += , cnt[i+] += ;
}
}
return false;
} bool judge() {
for(int i=; i<=; i++) {
if(cnt[i] >= ) {
cnt[i] -= ;
if(dfs()) return true;
cnt[i] += ;
}
}
return false;
} int main() {
char tmp[];
int cas = ;
while(scanf("%s", tmp)) {
if(tmp[] == '') break;
init();
id[] = find(tmp);
for(int i=; i<=; i++) {
scanf("%s", tmp);
id[i] = find(tmp);
}
printf("Case %d:", cas++);
bool flag = false;
for(int i=; i<=; i++) {
if(Mahjong[i][] == '') continue;
memset(cnt, , sizeof cnt);
for(int j=; j<=; j++) cnt[id[j]]++;
if(cnt[i] >= ) continue;
cnt[i]++;
if(judge()) {
flag = true;
printf(" %s", Mahjong[i]);
}
}
if(!flag) printf(" Not ready");
printf("\n");
}
return ;
}
上一篇:UVa中国麻将(Chinese Mahjong,Uva 11210)


下一篇:#C++初学记录(N皇后#回溯递归)