Mahjong is a favorite pastime in China. It can be played either with a set of mahjong tiles or a set of mahjong playing cards (sometimes spelled "kards" to distinguish them from the list of standard hands used in American mahjong). One brand of mahjong cards calls these Mhing. Playing cards are often used when travelling, as they take up less space and are lighter than their tile counterparts;however, they are usually of a lower quality. In this article, "tile" will be used to denote both playing cards and tiles.
There are many variations of mahjong. In many places, players often observe one version and are either unaware of other variations or claim that different versions are incorrect. Here, we are using the 13-tile version.
In order to simplify the problem. We only consider 13 tiles of Suit Character (named as each tile represents ten thousand coins, or one hundred strings of one hundred coins):
We use a single integer (1 to 9) to represent a tile (Character 1 to 9) in this problem.
In this simplified problem, consider below melds only(refer to Mahjong - Wikipedia for complete description):
- A Pong, or Pung, is a set of three identical tiles.
For example: 9 9 9; 7 7 7; - A Chow is a meld of three suited tiles in sequence.
For example: 1 2 3; 3 4 5; 7 8 9; 5 6 7; - An Eye is the pair, while not a meld (and thus cannot be declared or formed with a discard, except if completing the pair completes the hand), is the final component to the standard hand. It consists of any two identical tiles.
For example: 1 1; 7 7;
A player wins by creating a standard mahjong hand, which consists of a certain number of melds (four for 13-tile version) and a single Eye.
Your task is to figure out which tile can make you win according to 13 tiles you already have.
Note that you can't have more than 4 tiles with a same number at any time.
Input
Multi cases (no more than 25). Process to the end.
Each case has a single line containing 13 integers, which are tiles you already have.
Output
For each case, output tiles that can make you win in one line(sorted, smaller first), separated by one space. There is always a solution.
Sample Input
1 3 4 4 4 4 5 6 6 7 7 7 8
Sample Output
2
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[10],flag;
void dfs(int x,int y)
{
if(y>1||flag) return;
if(x==4&&y==1) {
flag=1;
return;
}
for(int i=1;i<=9;i++) {
if(a[i]>=2) {
a[i]-=2;
dfs(x,y+1);
a[i]+=2;
}
if(a[i]>=3) {
a[i]-=3;
dfs(x+1,y);
a[i]+=3;
}
if(i+2<=9&&a[i]>=1&&a[i+1]>=1&&a[i+2]>=1) {
a[i]-=1; a[i+1]-=1; a[i+2]-=1;
dfs(x+1,y);
a[i]+=1; a[i+1]+=1; a[i+2]+=1;
}
}
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
int i,first=1;
memset(a,0,sizeof(a));
a[t]++;
for(i=0;i<12;i++) scanf("%d",&t),a[t]++;
for(i=1;i<=9;i++) if(a[i]<4) {
flag=0;
a[i]++;
dfs(0,0);
if(flag) {
if(first) first=0;
else printf(" ");
printf("%d",i);
}
a[i]--;
}
printf("\n");
}
return 0;
}
心得:深搜时,要注意恢复先前改变的变量。
深搜最好不要返回bool类型