本文主要是介绍uva11210 Chinese Mahjong,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
接触计算机这么长时间(仔细想想,可能也就1年多吧),第一次觉得中国人还是能占到便宜的。。。。。。
简单的模拟,判断手中的麻将牌是否“下胡”以及缺什么牌,估计老外做这题会花上一段时间读题吧。
基本按照麻将的规则模拟:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;char hand[4][9],output[4][9][10]={{"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 a,b,ok,have;void dfs(int cur)
{if(ok==1)return ;if(cur==4){ok=1,have=1;printf(" %s",output[a][b]);}int i,j;for(i=0;i<4;i++){for(j=0;j<9;j++){if(hand[i][j]>=3){hand[i][j]-=3;dfs(cur+1);hand[i][j]+=3;}}}for(i=0;i<3;i++){for(j=0;j<7;j++){if(hand[i][j]>=1&&hand[i][j+1]>=1&&hand[i][j+2]>=1){hand[i][j]--,hand[i][j+1]--,hand[i][j+2]--;dfs(cur+1);hand[i][j]++,hand[i][j+1]++,hand[i][j+2]++;}}}
}void slove()
{int i,j,k,t;have=0;for(i=0;i<3;i++){for(j=0;j<9;j++){if(hand[i][j]==4)continue;hand[i][j]++;a=i,b=j;ok=0;for(k=0;k<4;k++)//可以是4{for(t=0;t<9;t++){if(ok==0&&hand[k][t]>=2){hand[k][t]-=2;dfs(0);hand[k][t]+=2;}}}hand[i][j]--;}}for(i=0;i<7;i++){if(hand[i][j]==4)continue;hand[3][i]++;a=3,b=i;ok=0;for(k=0;k<4;k++){for(t=0;t<9;t++){if(ok==0&&hand[k][t]>=2){hand[k][t]-=2;dfs(0);hand[k][t]+=2;}}}hand[3][i]--;}if(have==0)printf(" Not ready");printf("\n");
}int main()
{char input[10];int count=0,all=1,i,j;memset(hand,0,36);while(scanf("%s",input)&&input[0]!='0'){count++;if(input[1]=='T')hand[0][input[0]-'1']++;else if(input[1]=='S')hand[1][input[0]-'1']++;else if(input[1]=='W')hand[2][input[0]-'1']++;else if(strcmp(input,"DONG")==0)hand[3][0]++;else if(strcmp(input,"NAN")==0)hand[3][1]++;else if(strcmp(input,"XI")==0)hand[3][2]++;else if(strcmp(input,"BEI")==0)hand[3][3]++;else if(strcmp(input,"ZHONG")==0)hand[3][4]++;else if(strcmp(input,"FA")==0)hand[3][5]++;else if(strcmp(input,"BAI")==0)hand[3][6]++;if(count==13){//for(i=0;i<4;i++)//{// for(j=0;j<9;j++)// printf("%d",hand[i][j]);// puts("");// }printf("Case %d:",all++);slove();count=0;memset(hand,0,36);}}return 0;
}
这篇关于uva11210 Chinese Mahjong的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!