描述:麻将题目,其实就是判断麻将是否能不能胡的问题,不过要注意麻将每张牌只能重复四张,而且麻将牌中出现三个重复时要考虑一下是否直接剔除
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define NX 1000003
char str[14][6];
char temp[][6]= {"DONG","NAN","XI","BEI","ZHONG","FA","BAI"};
int flag;
int count[4][10],visit[14];
int cmp(const void *p1,const void *p2)
{
if(((char *)p1)[1]-((char *)p2)[1]==0)
return ((char *)p1)[0]-((char *)p2)[0];
else return ((char *)p1)[1]-((char *)p2)[1];
}
void dfs()
{
int sum=0;
for(int i=0; i<13; i++)
if(!visit[i])for(int j=i+1; j<13; j++)
if(!visit[j]&&(strcmp(str[i],str[j])==0||(str[j][0]-str[i][0]==1&&str[j][1]==str[i][1])))
for(int k=j+1; k<13; k++)
if(!visit[k]&&((strcmp(str[j],str[i])==0&&strcmp(str[j],str[k])==0)
||(str[j][0]-str[i][0]==1&&str[j][1]==str[i][1]&&
str[k][0]-str[j][0]==1&&str[k][1]==str[j][1])))
{
visit[i]=visit[j]=visit[k]=1;
sum=1;
dfs();
visit[i]=visit[j]=visit[k]=0;
}
//if(!sum)
{
int a=0,c[5],d[4],x,z[5];
for(int i=0; i<13&&a<5; i++) if(!visit[i]) c[a++]=i;
memcpy(z,c,sizeof(c));
if(a==1)
{
a=c[0];
int b=0;
for(int i=0; i<13; i++) if(strcmp(str[a],str[i])==0) b++;
if(b<4)
{
flag=1;
x=str[a][0]-'0';
if(str[a][0]>='1'&&str[a][0]<='9')
{
if(str[a][1]=='T') count[0][x]=1;
else if(str[a][1]=='S') count[1][x]=1;
else count[2][x]=1;
}
else for(int i=0; i<7; i++)
if(strcmp(temp[i],str[a])==0) count[3][i]=1;
}
}
else if(a==4)
{
int b=0;
for(int i=0; i<4; i++)
if(c[i]!=-1)for(int j=i+1; j<4; j++)
if(c[j]!=-1&&strcmp(str[c[i]],str[c[j]])==0)
{
if(!b)
{
d[0]=c[i];
d[1]=c[j];
}
else
{
d[2]=c[i];
d[3]=c[j];
}
b++;
c[i]=c[j]=-1;
}
if(b==2)
{
a=0;
for(int i=0; i<13; i++) if(strcmp(str[i],str[d[0]])==0) a++;
if(a<4)
{
flag=1;
a=d[0];
x=str[a][0]-'0';
if(str[a][0]>='1'&&str[a][0]<='9')
{
if(str[a][1]=='T') count[0][x]=1;
else if(str[a][1]=='S') count[1][x]=1;
else count[2][x]=1;
}
else for(int i=0; i<7; i++)
if(strcmp(temp[i],str[a])==0) count[3][i]=1;
}
a=0;
for(int i=0; i<13; i++) if(strcmp(str[i],str[d[3]])==0) a++;
if(a<4)
{
flag=1;
a=d[3];
x=str[a][0]-'0';
if(str[a][0]>='1'&&str[a][0]<='9')
{
if(str[a][1]=='T') count[0][x]=1;
else if(str[a][1]=='S') count[1][x]=1;
else count[2][x]=1;
}
else for(int i=0; i<7; i++)
if(strcmp(temp[i],str[a])==0) count[3][i]=1;
}
}
else if(b==1)
{
for(int i=0; i<4; i++)
if(c[i]!=-1)for(int j=i+1; j<4; j++)
if(c[j]!=-1)
{
if(str[c[i]][1]==str[c[j]][1]&&str[c[j]][0]-str[c[i]][0]==1)
{
if(str[c[i]][0]!='1')
{
char ch=str[c[i]][1];
a=str[c[i]][0]-'1';
int y=0;
for(int k=0; k<13; k++) if(str[k][0]==a+'0'&&str[k][1]==ch) y++;
if(y<4)
{
if(ch=='T') count[0][a]=1;
else if(ch=='S') count[1][a]=1;
else count[2][a]=1;
c[i]=-1;
flag=1;
}
}
if(str[c[j]][0]!='9')
{
char ch=str[c[j]][1];
a=str[c[j]][0]-'0'+1;
int y=0;
for(int k=0; k<13; k++) if(str[k][0]==a+'0'&&str[k][1]==ch) y++;
if(y<4)
{
if(ch=='T') count[0][a]=1;
else if(ch=='S') count[1][a]=1;
else count[2][a]=1;
c[j]=-1;
flag=1;
}
}
}
if(str[c[i]][1]==str[c[j]][1]&&str[c[j]][0]-str[c[i]][0]==2)
{
a=str[c[j]][0]-'1';
char ch=str[c[i]][1];
int y=0;
for(int k=0; k<13; k++) if(str[k][0]==a+'0'&&str[k][1]==ch) y++;
if(y<4)
{
flag=1;
if(ch=='T') count[0][a]=1;
else if(ch=='S') count[1][a]=1;
else count[2][a]=1;
c[i]=c[j]=-1;
}
}
}
}
}
}
}
int main()
{
// freopen("a.txt","r",stdin);
int t=1;
while(scanf("%s",str[0])!=EOF)
{
if(str[0][0]=='0') break;
for(int i=1; i<13; i++) scanf("%s",str[i]);
qsort(str,13,sizeof(str[0]),cmp);
memset(count,0,sizeof(count));
memset(visit,0,sizeof(visit));
flag=0;
dfs();
printf("Case %d:",t++);
if(flag)
{
for(int i=0; i<4; i++)
for(int j=1; j<10; j++)
if(count[i][j])
{
if(!i) printf(" %dT",j);
else if(i==1) printf(" %dS",j);
else if(i==2)printf(" %dW",j);
else printf(" %s",temp[j]);
}
printf("\n");
}
else printf(" Not ready\n");
}
return 0;
}
11210 - Chinese Mahjong
最新推荐文章于 2020-12-05 07:04:13 发布
这是一个关于麻将算法的程序实现,通过判断麻将牌是否能组成胡牌组合。程序中使用了深度优先搜索(DFS)策略,考虑了麻将牌的重复限制,如三张相同的牌或顺子等。代码包括了比较函数、DFS遍历和核心的胡牌逻辑判断。
摘要由CSDN通过智能技术生成