UVa中国麻将(Chinese Mahjong,Uva 11210)
Posted InWILL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa中国麻将(Chinese Mahjong,Uva 11210)相关的知识,希望对你有一定的参考价值。
简单的回溯题
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 char *mahjong[]={ 8 "1T","2T","3T","4T","5T","6T","7T","8T","9T", 9 "1S","2S","3S","4S","5S","6S","7S","8S","9S", 10 "1W","2W","3W","4W","5W","6W","7W","8W","9W", 11 "DONG","NAN","XI","BEI", 12 "ZHONG","FA","BAI" 13 }; 14 15 int convert(char *s)//映射处理 16 { 17 for(int i=0;i<34;i++) 18 if(strcmp(mahjong[i],s)==0) 19 return i; 20 return -1; 21 } 22 23 int c[34]; 24 25 bool search(int dep) 26 { 27 for(int i=0;i<34;i++)//枚举刻子 28 if(c[i]>=3) 29 { 30 if(dep==3) return true; 31 c[i]-=3; 32 if(search(dep+1)) return true; 33 c[i]+=3; 34 } 35 for(int i=0;i<34;i++)//枚举顺子 36 if(i%9<=6&&c[i]>=1&&c[i+1]>=1&&c[i+2]>=1) 37 { 38 if(dep==3) return true; 39 c[i]--;c[i+1]--;c[i+2]--; 40 if(search(dep+1)) return true; 41 c[i]++;c[i+1]++;c[i+2]++; 42 } 43 return false; 44 } 45 46 bool check() 47 { 48 for(int i=0;i<34;i++)//枚举将牌 49 if(c[i]>=2) 50 { 51 c[i]-=2; 52 if(search(0)) return true; 53 c[i]+=2; 54 } 55 return false; 56 } 57 58 int main() 59 { 60 int casen=0; 61 int mj[15]; 62 char s[100]; 63 bool ok; 64 while(cin>>s) 65 { 66 if(s[0]==‘0‘) break; 67 printf("Case %d:",++casen); 68 mj[0]=convert(s); 69 for(int i=1;i<13;i++) 70 { 71 cin>>s; 72 mj[i]=convert(s); 73 } 74 ok=false; 75 for(int i=0;i<34;i++)//枚举听牌 76 { 77 memset(c,0,sizeof(c)); 78 for(int j=0;j<13;j++) c[mj[j]]++; 79 if(c[i]>=4) continue; 80 c[i]++; 81 if(check()) 82 { 83 ok=true; 84 printf(" %s",mahjong[i]); 85 } 86 c[i]--; 87 } 88 if(!ok) 89 printf(" Not ready"); 90 printf("\n"); 91 } 92 return 0; 93 }
以上是关于UVa中国麻将(Chinese Mahjong,Uva 11210)的主要内容,如果未能解决你的问题,请参考以下文章