hdu 4431 Mahjong (模拟+搜索)

本文详细解析了一种四人麻将游戏的算法实现,介绍了如何判断手中的牌是否能构成有效的胡牌组合,并通过具体的代码示例展示了算法的设计思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mahjong

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2406    Accepted Submission(s): 500


Problem Description
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.)
 

Input
The input data begins with a integer T(1≤T≤20000). Next are T cases, each of which contains 13 tiles. The description of every tile is as above.
 

Output
For each cases, if there actually exists some tiles that can form a winning state with the 13 tiles given, print the number first and then print all those tiles in order as the description order of tiles above. Otherwise print a line "Nooten"(without quotation marks).
 

Sample Input
  
2 1s 2s 3s 2c 2c 2c 2p 3p 5m 6m 7m 1p 1p 1p 1p 2p 3p 4s 5s 6s 7c 7c 3s 3s 2m 2m
 

Sample Output
  
2 1p 4p Nooten
 

Source

题意:
打麻将,给你一副牌,问你能和(hu)什么。注意对对和、十三幺就ok了。

思路:
手上的牌有十三个,对可能和的牌枚举加进来,现在就有十四个了。然后取一对出来(eyes),剩下的十二个全是句子(meld )就满足条件。

注意:
同一个牌若有3个以上,则它组成句子的情况有几种,要讨论一下。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 15
using namespace std;

int n,ans,xxc,flag;
int m[maxn],s[maxn],p[maxn],c[maxn];
int tm[maxn],ts[maxn],tp[maxn],tc[maxn];
int ttm[maxn],tts[maxn],ttp[maxn],ttc[maxn];
int hm[maxn],hs[maxn],hp[maxn],hc[maxn];
char ins[maxn];
int state[maxn];
vector<int>am,as,ap,ac;
struct Node
{
    int id;
    char op;
} cur[maxn];

bool judge()
{
    int i,j,cnt=0;
    for(i=1; i<=9; i++)
    {
        if(tm[i]==2) cnt++;
        if(ts[i]==2) cnt++;
        if(tp[i]==2) cnt++;
        if(i<=7&&tc[i]==2) cnt++;
    }
    if(cnt==7) return true ;
    for(i=2;i<=8;i++)
    {
        if(tm[i]) return false ;
        if(ts[i]) return false ;
        if(tp[i]) return false ;
    }
    if(tm[1]&&tm[9]&&ts[1]&&ts[9]&&tp[1]&&tp[9]&&tc[1]&&tc[2]&&tc[3]&&tc[4]&&tc[5]&&tc[6]&&tc[7]) return true ;
    return false ;
}
bool valf(int hem[],int hes[],int hep[],int hec[])
{
    int i,j;
    for(i=1; i<=7; i++)
    {
        while(hem[i]&&hem[i+1]&&hem[i+2]) hem[i]--,hem[i+1]--,hem[i+2]--;
        while(hes[i]&&hes[i+1]&&hes[i+2]) hes[i]--,hes[i+1]--,hes[i+2]--;
        while(hep[i]&&hep[i+1]&&hep[i+2]) hep[i]--,hep[i+1]--,hep[i+2]--;
    }
    for(i=1; i<=9; i++)
    {
        if(hem[i]||hes[i]||hep[i]) return false ;
        if(i<=7&&hec[i]) return false ;
    }
    return true ;
}
void dfs(int pos)
{
    if(flag) return ;
    if(pos==xxc+1)
    {
        memcpy(hm,ttm,sizeof(ttm));
        memcpy(hs,tts,sizeof(tts));
        memcpy(hp,ttp,sizeof(ttp));
        memcpy(hc,ttc,sizeof(ttc));
        for(int i=1; i<=xxc; i++)
        {
            if(state[i])
            {
                int nid=cur[i].id;
                char nop=cur[i].op;
                if(nop=='m') hm[nid]-=3;
                else if(nop=='s') hs[nid]-=3;
                else if(nop=='p') hp[nid]-=3;
                else hc[nid]-=3;
            }
        }
        if(valf(hm,hs,hp,hc)) flag=1;
        return ;
    }
    state[pos]=1;
    dfs(pos+1);
    state[pos]=0;
    dfs(pos+1);
}
bool hehe()
{
    int i,j;
    memcpy(ttm,tm,sizeof(ttm));
    memcpy(tts,ts,sizeof(tts));
    memcpy(ttp,tp,sizeof(ttp));
    memcpy(ttc,tc,sizeof(ttc));
    xxc=0;
    for(i=1; i<=9; i++)
    {
        if(ttm[i]>=3)
        {
            xxc++;
            cur[xxc].id=i;
            cur[xxc].op='m';
        }
        if(tts[i]>=3)
        {
            xxc++;
            cur[xxc].id=i;
            cur[xxc].op='s';
        }
        if(ttp[i]>=3)
        {
            xxc++;
            cur[xxc].id=i;
            cur[xxc].op='p';
        }
        if(i<=7&&ttc[i]>=3) ttc[i]-=3;
    }
    if(xxc==0)
    {
        if(valf(ttm,tts,ttp,ttc)) return true ;
    }
    else
    {
        flag=0;
        memset(state,0,sizeof(state));
        dfs(1);
        if(flag) return true ;
    }
    return false ;
}
bool work()
{
    int i,j;
    if(judge()) return true ;
    for(i=1; i<=9; i++) // 把将取出
    {
        if(tm[i]>=2)
        {
            tm[i]-=2;
            if(hehe())
            {
                tm[i]+=2;
                return true ;
            }
            tm[i]+=2;
        }
    }
    for(i=1; i<=9; i++) // 把将取出
    {
        if(ts[i]>=2)
        {
            ts[i]-=2;
            if(hehe())
            {
                ts[i]+=2;
                return true ;
            }
            ts[i]+=2;
        }
    }
    for(i=1; i<=9; i++) // 把将取出
    {
        if(tp[i]>=2)
        {
            tp[i]-=2;
            if(hehe())
            {
                tp[i]+=2;
                return true ;
            }
            tp[i]+=2;
        }
    }
    for(i=1; i<=7; i++) // 把将取出
    {
        if(tc[i]>=2)
        {
            tc[i]-=2;
            if(hehe())
            {
                tc[i]+=2;
                return true ;
            }
            tc[i]+=2;
        }
    }
    return false ;
}
void solve()
{
    int i,j,t1,t2;
    memcpy(ts,s,sizeof(ts));
    memcpy(tp,p,sizeof(tp));
    memcpy(tc,c,sizeof(tc));
    for(i=1; i<=9; i++)
    {
        t1=i-1;
        if(t1==0) t1=9;
        t2=i+1;
        if(t2==10) t2=1;
        if(!m[t1]&&!m[i]&&!m[t2]) continue ;
        memcpy(tm,m,sizeof(tm));
        tm[i]++;
        if(tm[i]>4)
        {
            tm[i]--;
            continue ;
        }
        if(work())
        {
            ans++;
            am.push_back(i);
        }
        tm[i]--;
    }
    memcpy(tm,m,sizeof(tm));
    memcpy(tp,p,sizeof(tp));
    memcpy(tc,c,sizeof(tc));
    for(i=1; i<=9; i++)
    {
        t1=i-1;
        if(t1==0) t1=9;
        t2=i+1;
        if(t2==10) t2=1;
        if(!s[t1]&&!s[i]&&!s[t2]) continue ;
        memcpy(ts,s,sizeof(ts));
        ts[i]++;
        if(ts[i]>4)
        {
            ts[i]--;
            continue ;
        }
        if(work())
        {
            ans++;
            as.push_back(i);
        }
        ts[i]--;
    }
    memcpy(tm,m,sizeof(tm));
    memcpy(ts,s,sizeof(ts));
    memcpy(tc,c,sizeof(tc));
    for(i=1; i<=9; i++)
    {
        t1=i-1;
        if(t1==0) t1=9;
        t2=i+1;
        if(t2==10) t2=1;
        if(!p[t1]&&!p[i]&&!p[t2]) continue ;
        memcpy(tp,p,sizeof(tp));
        tp[i]++;
        if(tp[i]>4)
        {
            tp[i]--;
            continue ;
        }
        if(work())
        {
            ans++;
            ap.push_back(i);
        }
        tp[i]--;
    }
    memcpy(tm,m,sizeof(tm));
    memcpy(ts,s,sizeof(ts));
    memcpy(tp,p,sizeof(tp));
    for(i=1; i<=7; i++)
    {
        t1=i-1;
        if(t1==0) t1=9;
        t2=i+1;
        if(t2==10) t2=1;
        if(!c[t1]&&!c[i]&&!c[t2]) continue ;
        memcpy(tc,c,sizeof(tc));
        tc[i]++;
        if(tc[i]>4)
        {
            tc[i]--;
            continue ;
        }
        if(work())
        {
            ans++;
            ac.push_back(i);
        }
        tc[i]--;
    }
}
int main()
{
    int i,j,t,cnt,sz;
    scanf("%d",&t);
    while(t--)
    {
        memset(m,0,sizeof(m));
        memset(s,0,sizeof(s));
        memset(p,0,sizeof(p));
        memset(c,0,sizeof(c));
        am.clear();
        as.clear();
        ap.clear();
        ac.clear();
        for(i=1; i<=13; i++)
        {
            scanf("%s",ins);
            if(ins[1]=='m') m[ins[0]-'0']++;
            else if(ins[1]=='s') s[ins[0]-'0']++;
            else if(ins[1]=='p') p[ins[0]-'0']++;
            else c[ins[0]-'0']++;
        }
        ans=0;
        solve();
        if(ans==0) printf("Nooten\n");
        else
        {
            printf("%d",ans);
            sz=am.size();
            for(i=0; i<sz; i++)
            {
                printf(" %dm",am[i]);
            }
            sz=as.size();
            for(i=0; i<sz; i++)
            {
                printf(" %ds",as[i]);
            }
            sz=ap.size();
            for(i=0; i<sz; i++)
            {
                printf(" %dp",ap[i]);
            }
            sz=ac.size();
            for(i=0; i<sz; i++)
            {
                printf(" %dc",ac[i]);
            }
            printf("\n");
        }
    }
    return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值