UVA11210 Chinese Mahjong

链接

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2151

题解

爆搜题,没有任何剪枝
对每种情形枚举如何处理它的最小的牌,要么组成顺子,要么组成刻子
刘汝佳的思路:枚举深度(即3,2,1),枚举出牌方式,然后枚举牌

代码

//爆搜 
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#define maxpos 34
using namespace std;
int res[maxpos+10];
bool success;
map<string,int> table;
map<int,string> intable;
bool check_shun(int x)
{
    int i;
    if(1<=x and x<=7)return res[x] and res[x+1] and res[x+2];
    if(10<=x and x<=16)return res[x] and res[x+1] and res[x+2];
    if(19<=x and x<=25)return res[x] and res[x+1] and res[x+2];
    return false;
}
void make_table()
{
    table["1T"]=1;
    table["2T"]=2;
    table["3T"]=3;
    table["4T"]=4;
    table["5T"]=5;
    table["6T"]=6;
    table["7T"]=7;
    table["8T"]=8;
    table["9T"]=9;
    table["1S"]=10;
    table["2S"]=11;
    table["3S"]=12;
    table["4S"]=13;
    table["5S"]=14;
    table["6S"]=15;
    table["7S"]=16;
    table["8S"]=17;
    table["9S"]=18;
    table["1W"]=19;
    table["2W"]=20;
    table["3W"]=21;
    table["4W"]=22;
    table["5W"]=23;
    table["6W"]=24;
    table["7W"]=25;
    table["8W"]=26;
    table["9W"]=27;
    table["DONG"]=28;
    table["NAN"]=29;
    table["XI"]=30;
    table["BEI"]=31;
    table["ZHONG"]=32;
    table["FA"]=33;
    table["BAI"]=34;
    map<string,int>::iterator it;
    for(it=table.begin();it!=table.end();it++)intable[it->second]=it->first;
}
void dfs(int pos)
{
    int i;
    if(pos>maxpos)
    {
        for(i=1;i<=maxpos;i++)if(res[i])break;
        if(i>maxpos)success=1;
        return;
    }
    if(res[pos]==0){dfs(pos+1);return;}
    //顺子
    if(check_shun(pos))
    {
        for(i=pos;i<=pos+2;i++)res[i]--;
        dfs(pos);
        for(i=pos;i<=pos+2;i++)res[i]++;
        if(success)return;
    }
    //三连 
    if(res[pos]>=3)
    {
        res[pos]-=3;
        dfs(pos);
        res[pos]+=3;
        if(success)return;
    }
}
int main()
{
    int i, j, c, flag, cnt;
    make_table();
    string str;
    for(c=1;;c++)
    {
        for(i=1;i<=maxpos;i++)res[i]=0;
        for(i=1;i<=13 and str!="0";i++)cin>>str, res[table[str]]++;
        if(str=="0")return 0;
        printf("Case %d: ",c);
        flag=0;
        for(i=1;i<=maxpos;i++)
            if(res[i]<4)
            {
                res[i]++;
                success=0;
                for(j=1;j<=maxpos and !success;j++)
                    if(res[j]>=2)
                    {
                        res[j]-=2;
                        dfs(1);
                        res[j]+=2;
                    }
                res[i]--;
                if(success)
                { 
                    if(!flag)cout<<intable[i], flag=1;
                    else cout<<" "<<intable[i];
                }
            }
        if(!flag)cout<<"Not ready";
        cout<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/fsahfgsadhsakndas/article/details/81003195
今日推荐