【题解】 UVa11210 Chinese Mahjong

pdf传送门


直接模拟+搜索,考虑一下选哪一个是将,然后搜出顺子和刻子,最后判断一下可不可行就好了!

#include<stdio.h>
#include<string.h>
const 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"
};
char s[15];
int Case,c[36],mj[15];
int init(char *S){
	for(int i=0;i<34;i++)
		if(strcmp(S,mahjong[i])==0)return i;
	return -1;
}
bool search(int dep){
	for(int i=0;i<34;i++)
		if(c[i]>=3){
			if(dep==3)return true;
			c[i]-=3;
			if(search(dep+1))return true;
			c[i]+=3;
		}
	for(int i=0;i<25;i++)
		if(i%9<=6 && c[i] && c[i+1] && c[i+2]){
			if(dep==3)return true;
			c[i]--;c[i+1]--;c[i+2]--;
			if(search(dep+1))return true;
			c[i]++;c[i+1]++;c[i+2]++;
		}
	return false;
}
bool check(){
	for(int i=0;i<34;i++)
		if(c[i]>=2){
			c[i]-=2;
			if(search(0))return true;
			c[i]+=2;
		}
	return false;
}
int main(){
	while(scanf("%s",&s)!=EOF){
		if(s[0]=='0')break;
		printf("Case %d:",++Case);
		mj[0]=init(s);
		for(int i=1;i<13;i++){
			scanf("%s",&s);
			mj[i]=init(s);
		}
		int flag=0;
		for(int i=0;i<34;i++){
			memset(c,0,sizeof(c));
			for(int j=0;j<13;j++)c[mj[j]]++;
			if(c[i]>3)continue;
			c[i]++;
			if(check()){
				flag=1;
				printf(" %s",mahjong[i]);
			}
			c[i]--;
		}
		if(!flag)printf(" Not ready");
		puts("");
	}
	return 0;
}
posted @ 2018-11-09 09:42  cjgjh  阅读(226)  评论(0编辑  收藏  举报