2019计蒜之道 B:个性化评测系统

比赛的时候没有想清楚,直接排序然后从前向后dfs,一直卡着,就是没有想到其他的情况。其实有可能是223344这样的,最后写的时候忘记处理了222233334444情况,还是看别人博客才发现的。还是太菜了,继续努力。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#define ll long long

using namespace std;

char mian[10]="mspz";
map<string,int> majong[4];
map<string,int>::iterator itr;
string tmp;
int cnt[4][14];

int dfs(int x,int c1,int c2)
{
    int i,j,flag=0;
    if(c1==4 && c2==1)
        return 1;
    else if(c1>4 || c2>1 || x>=14)
        return 0;
    if(c2==0)
    {
        for(i=0;i<4;i++)
            for(j=1;j<=9;j++)
                if(cnt[i][j]>=2)
                {
                    cnt[i][j]-=2;
                    flag|=dfs(x+2,c1,c2+1);
                    cnt[i][j]+=2;
                    if(flag)
                        return 1;
                }
        return 0;
    }
    else
    {
        for(i=0;i<4;i++)
            for(j=1;j<=9;j++)
            {
                if(cnt[i][j]==0)
                    continue;
                else if(cnt[i][j]==1)
                {
                    if(i!=3 && cnt[i][j+1]>0 && cnt[i][j+2]>0)
                    {
                        cnt[i][j]--,cnt[i][j+1]--,cnt[i][j+2]--;
                        flag|=dfs(x+3,c1+1,c2);
                        cnt[i][j]++,cnt[i][j+1]++,cnt[i][j+2]++;
                        if(flag)
                            return 1;
                    }
                    else
                        return 0;
                }
                else if(cnt[i][j]==2)
                {
                    if(i!=3 && cnt[i][j+1]>=2 && cnt[i][j+2]>=2)
                    {
                        cnt[i][j]-=2,cnt[i][j+1]-=2,cnt[i][j+2]-=2;
                        flag|=dfs(x+6,c1+2,c2);
                        cnt[i][j]+=2,cnt[i][j+1]+=2,cnt[i][j+2]+=2;
                        if(flag)
                            return 1;
                    }
                    else
                        return 0;
                }
                else if(cnt[i][j]==3)
                {
                    cnt[i][j]-=3;
                    flag|=dfs(x+3,c1+1,c2);
                    cnt[i][j]+=3;
                    if(flag)
                        return 1;
                    if(i!=3 && cnt[i][j+1]==3 && cnt[i][j+2]==3)
                    {
                        cnt[i][j]-=3,cnt[i][j+1]-=3,cnt[i][j+2]-=3;
                        flag|=dfs(x+9,c1+3,c2);
                        cnt[i][j]+=3,cnt[i][j+1]+=3,cnt[i][j+2]+=3;
                        if(flag)
                            return 1;
                    }
                    else
                        return 0;
                }
                else if(cnt[i][j]==4)
                {
                    cnt[i][j]-=3;
                    flag|=dfs(x+3,c1+1,c2);
                    cnt[i][j]+=3;
                    if(flag)
                        return 1;
                    if(i!=3 && cnt[i][j+1]==4 && cnt[i][j+2]==4)
                    {
                        cnt[i][j]-=4,cnt[i][j+1]-=4,cnt[i][j+2]-=4;
                        flag|=dfs(x+12,c1+3,c2);
                        cnt[i][j]+=4,cnt[i][j+1]+=4,cnt[i][j+2]+=4;
                        if(flag)
                            return 1;
                    }
                    else
                        return 0;
                }
            }
    }
}

inline void add(string t)
{
    switch(t[1])
    {
    case 'm':
        cnt[0][t[0]-'0']++; break;
    case 's':
        cnt[1][t[0]-'0']++; break;
    case 'p':
        cnt[2][t[0]-'0']++; break;
    case 'z':
        cnt[3][t[0]-'0']++; break;
    };
}

inline int check()
{
    int i,j;
    for(i=0;i<4;i++)
        for(j=1;j<=9;j++)
            if(cnt[i][j]>4)
                return 0;
    return 1;
}

int main()
{
    int i,j;
    string str;
    while(cin>>str)
    {
        memset(cnt,0,sizeof(cnt));
        add(str);
        for(i=0;i<12;i++)
            cin>>str, add(str);
        /*for(i=0;i<4;i++)
        {
            for(j=1;j<=9;j++)
                cout<<cnt[i][j]<<' ';
            cout<<endl;
        }*/

        for(i=0;i<3;i++)
            for(j=1;j<=9;j++)
            {
                tmp="";
                tmp+='0'+j;
                tmp+=mian[i];
                cnt[i][j]++;
                if(check() && dfs(0,0,0))
                    cout<<tmp<<endl;
                cnt[i][j]--;
            }
        for(j=1;j<=7;j++)
        {
            tmp="";
            tmp+='0'+j;
            tmp+=mian[3];
            cnt[3][j]++;
            if(check() && dfs(0,0,0))
                cout<<tmp<<endl;
            cnt[3][j]--;
        }
    }
    return 0;
}


/*

1s 2s 3s 4s 5s 6s 7s 8s 9s 1z 1z 3p 4p


*/

猜你喜欢

转载自www.cnblogs.com/canchan/p/11072521.html