Description
Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this problem, we concentrate on Japanese Mahjong, a variation of mahjong. For brief, all of the word mahjong mentioned following refer to Japanese Mahjong.
Japanese mahjong is usually played with 136 tiles, which can be organized into several categories:
- Suited tiles. All suited tiles are of a rank and a suit.There are three suits of tiles, with ranks ranging from one to nine. There are four tiles of each rank and suit combination, thus there are 36 tiles in a suit, and 108 suited tiles in total.
- The circle suit
- The bamboo suit
- The character suit
- The circle suit
- Honor tiles. Honor Tiles are tiles that do not have a rank or suit. They are divided into two categories. There are four types of Wind tiles and three types of Dragon tiles, with four of each type of honor tile. Thus, there are 16 wind tiles and 12 Dragon tiles for 28 honor tiles.
- Wind tiles. The Wind tiles consist of four kinds of tile: East, South, West, and North.
- Dragon tiles. The Dragon titles consist of three types of tile: Red, Green, White.
- Wind tiles. The Wind tiles consist of four kinds of tile: East, South, West, and North.
A winning hand consists of fourteen tiles, which is made of four melds (a specific pattern of three pieces) and the eyes (a pair of two identical pieces). The definition of melds and eyes is given as followed:
- Melds are listed as followed:
- Pong is a set of three identical tiles. You can form a pong with any tile. The tiles must be identical (you cannot mix suits). For example:
- Kong is a set of four identical tiles, which is similar to Pong. For example:
- Chow is a meld of three suited tiles in sequence. The meld must be in absolute numerical sequence. There is no skipping of numbers, nor does 9 loop around to 1. The sequence must be in the same suit. Honours cannot be used to make chows. For example:
- Pong is a set of three identical tiles. You can form a pong with any tile. The tiles must be identical (you cannot mix suits). For example:
- Eyes, also known as a pair, are two identical tiles which are a component to the standard hand. For example:
When a hand is one tile short of winning, the hand is said to be a ready hand, or more figuratively, "on the pot". The player holding a ready hand is said to be waiting for certain tiles. Now, given thirteen tiles, can you answer how many types of tiles you are waiting for awinning hand? If the given hand were not a ready hand, output an integer zero instead.
Input
There are multiple cases. Each case consists of 26 characters in one line, describing thirteen tiles. The manner for describing each type of tile is:
- Two characters stand for one tile.
- For Suited tiles, the first is a integer ranged from one to nine, the tile's rank, and the second is a character: 'p' for the circle suit, 's'for the bamboo suit, and 'm' for the character suit.
- For Honor tiles, the first is a integer: from one to seven stands for East, South, West, North, White, Green, and Red respectively. The second one is always 'z'.
Output
For each case, first output the number of types of tiles you are waiting for in one line. Then output each type of tile you are waiting for in the manner described above. output them in this fixed order: 'm', 'p', 's', 'z', and for each suit, smaller rank first (Honor tiles is similar tosuited tiles, only using the integer standing for in above manner instead of suited rank).
Sample Input
1s1s1s2p3p4p6m7m8m1z1z1z2z 1s1s1s2p3p4p6m7m8m1z1z1z9m 1s1s1s2p3p4p6m7m8m1z1z1z1z 1s2s3s1s2s3s2s3s7s8s9s6z6z
Sample Output
1 2z 2 6m9m 0 3 1s4s6z
Hint
- The hand in the first picture is not a winning hand indeed, because there are fifteen tiles. If 1m or 9m is droped, it will be a winning hand.
- Note that the input tiles is not ensured in the order of output.
References
Mahjong - Wikipedia, the free encyclopedia
1 #include <vector> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 6 using namespace std; 7 8 char str[15]; 9 10 int cnt[4][14]; 11 12 int ctoi(char c) 13 { 14 if(c == 'm') return 0; 15 if(c == 'p') return 1; 16 if(c == 's') return 2; 17 if(c == 'z') return 3; 18 } 19 20 int itoc(int i) 21 { 22 if(i == 0) return 'm'; 23 if(i == 1) return 'p'; 24 if(i == 2) return 's'; 25 if(i == 3) return 'z'; 26 } 27 28 bool dfs() 29 { 30 for(int i=0;i<4;i++){ 31 for(int j=1;j<=(i==3?7:9);j++){ 32 if(!cnt[i][j]) 33 continue; 34 if(cnt[i][j] >= 3){ 35 cnt[i][j] -= 3; 36 if(dfs()){ 37 cnt[i][j] += 3; 38 return true; 39 } 40 cnt[i][j] += 3; 41 } 42 if(i != 3 && cnt[i][j] && cnt[i][j+1] && cnt[i][j+2]) 43 { 44 cnt[i][j]--;cnt[i][j+1]--;cnt[i][j+2]--; 45 if(dfs()){ 46 cnt[i][j]++;cnt[i][j+1]++;cnt[i][j+2]++; 47 return true; 48 } 49 cnt[i][j]++;cnt[i][j+1]++;cnt[i][j+2]++; 50 } 51 return false; 52 } 53 } 54 return true; 55 } 56 57 bool win() 58 { 59 for(int i=0;i<4;i++){ 60 for(int j=1;j<=(i==3?7:9);j++){ 61 if(cnt[i][j] >= 2){ 62 cnt[i][j] -= 2; 63 if(dfs()){ 64 cnt[i][j] += 2; 65 return true; 66 } 67 cnt[i][j] += 2; 68 } 69 } 70 } 71 return false; 72 } 73 74 int main() 75 { 76 while(~scanf("%s",str)) 77 { 78 memset(cnt,0,sizeof(cnt)); 79 for(int i=0;str[i];i+=2){ 80 cnt[ ctoi(str[i+1]) ][ str[i]-'0' ]++; 81 } 82 vector< pair<int,int> > ans; 83 for(int i=0;i<4;i++){ 84 for(int j=1;j<=(i==3?7:9);j++){ 85 if(cnt[i][j] == 4) 86 continue; 87 cnt[i][j]++; 88 if(win()) 89 ans.push_back(make_pair(i,j)); 90 cnt[i][j]--; 91 } 92 } 93 printf("%d",(int)ans.size()); 94 if((int)ans.size()){ 95 putchar(' '); 96 for(int i=0;i<(int)ans.size();i++) 97 printf("%d%c",ans[i].second,itoc(ans[i].first)); 98 } 99 puts(""); 100 } 101 return 0; 102 }