【HDU 4431】Mahjong

传送门

题目描述

Japanese Mahjong is a four-player game. The game needs four people to sit around a desk and play with a set of Mahjong tiles. A set of Mahjong tiles contains four copies of the tiles described next: 


One to nine Man, which we use 1m to 9m to represent; 


One to nine Sou, which we use 1s to 9s to represent; 


One to nine Pin, which we use 1p to 9p to represent; 


Character tiles, which are:Ton, Nan, Sei, Pei, Haku, Hatsu, Chun, which we use 1c to 7c to represent. 

A winning state means a set of 14 tiles that normally contains a pair of same tiles (which we call "eyes") and four melds. A meld is formed by either three same tiles(1m, 1m, 1m or 2c, 2c, 2c for example) or three continuous non-character tiles(1m, 2m, 3m or 5s, 6s, 7s for example). 

However, there are two special winning states that are different with the description above, which are: 
"Chii Toitsu", which means 7 different pairs of tiles; 
"Kokushi Muso", which means a set of tiles that contains all these tiles: 1m, 9m, 1p, 9p, 1s, 9s and all 7 character tiles. And the rest tile should also be one of the 13 tiles above. 

And the game starts with four players receiving 13 tiles. In each round every player must draw one tile from the deck one by one. If he reaches a winning state with these 14 tiles, he can say "Tsu Mo" and win the game. Otherwise he should discard one of his 14 tiles. And if the tile he throws out can form a winning state with the 13 tiles of any other player, the player can say "Ron" and win the game. 

Now the question is, given the 13 tiles you have, does there exist any tiles that can form a winning state with your tiles? 

(Notes: Some of the pictures and descriptions above come from Wikipedia.) 

题目翻译

解题思路

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int T,mj[50],tmp[50],ans[50],cnt;
 7 inline void ini(){
 8     cnt=0;
 9     memset(mj,0,sizeof(mj));
10     memset(tmp,0,sizeof(tmp));
11     memset(ans,0,sizeof(ans));
12 }
13 inline void print(int num){
14     if(num>=1&&num<=9)printf("%dm",num);
15     else if(num>=11&&num<=19)printf("%ds",num-10);
16     else if(num>=21&&num<=29)printf("%dp",num-20);
17     else printf("%dc",num-30);
18 }
19 inline bool judge1(){
20     int j[50];
21     for(register int pa=1;pa<=37;pa++){
22         int f=0;
23         if(tmp[pa]>=2){
24             memcpy(j,tmp,sizeof(tmp));
25             j[pa]-=2;
26             for(register int i=1;i<=37;i++){
27                 if(j[i]>=3)j[i]-=3;
28                 if(j[i]){
29                     if(i>=31){
30                         f=1;
31                     }
32                     if(j[i+1]<j[i]||j[i+2]<j[i]){
33                         f=1;
34                     }    
35                     else j[i+1]-=j[i],j[i+2]-=j[i],j[i]=0;
36                 }
37             }
38         }
39         else f=1;
40         if(f==0)return 1;
41     }
42     return 0;
43 }
44 inline bool judge2(){
45     int cnt=0;
46     for(register int i=1;i<=37;i++){
47         if(tmp[i]==2)cnt++;    
48     }
49     if(cnt==7)return 1;
50     return 0;
51 }
52 inline bool judge3(){
53     if(!tmp[1]||!tmp[9]||!tmp[11]||!tmp[19]||!tmp[21]||!tmp[29])return 0;
54     for(register int i=31;i<=37;i++){
55         if(!tmp[i])return 0;
56     }
57     for(register int i=1;i<=29;i++){
58         if(i==1||i==9||i==11||i==19||i==21||i==29)continue;
59         if(tmp[i])return 0;
60     }
61     return 1;
62 }
63 inline bool check(){
64     if(judge1()||judge2()||judge3())return 1;
65     return 0;
66 }
67 int main(){
68     cin>>T;
69     for(register int t=1;t<=T;t++){
70         ini();
71         register string s; register char ch; register int num;
72         for(register int i=1;i<=13;i++){
73             cin>>s;   ch=s[1]; num=s[0]-'0';
74             if(ch=='s')num+=10;
75             else if(ch=='p')num+=20;
76             else if(ch=='c')num+=30;
77             mj[num]++;
78         }
79         for(register int i=1;i<=37;i++){
80             if(i==10||i==20||i==30||mj[i]==4)continue;
81             memcpy(tmp,mj,sizeof(mj));
82             tmp[i]++;
83             if(check())ans[++cnt]=i;
84         }
85         if(cnt){
86             printf("%d ",cnt);
87             for(register int i=1;i<cnt;i++)print(ans[i]),putchar(' ');
88             print(ans[cnt]),putchar('\n');
89         }
90         else printf("Nooten\n");
91     }
92 }

猜你喜欢

转载自www.cnblogs.com/Fang-Hao/p/9164383.html
hdu