2019計蒜之道 B:個性化評測系統

比賽的時候沒有想清楚,直接排序而後從前向後dfs,一直卡着,就是沒有想到其餘的狀況。其實有多是223344這樣的,最後寫的時候忘記處理了222233334444狀況,仍是看別人博客才發現的。仍是太菜了,繼續努力。ios

 

#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


*/

spa

相關文章
相關標籤/搜索