2012 天津赛区 A题 hdu 4431

http://acm.hdu.edu.cn/showproblem.php?pid=4431

题意:给13个牌,问你可以糊那些牌。

思路:现场队友提醒就知道思路了,枚举每一张牌,看能不能win。却被麻将的规则虐了,题目好像没说下面的数据呀:

1s 1s 1m 9m 1p 9p 1c 2c 3c 4c 5c 6c 7c   (糊9s)

View Code
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<set>
#include<utility>
using namespace std;

int as[16][16];

set< pair<int,int> > bs;
set< pair<int,int> >::iterator p;

int id(char c)
{
    if(c=='m')return 1;
    if(c=='s')return 2;
    if(c=='p')return 3;
    return 4;
}
char fid(int c)
{
    if(c==1)return 'm';
    if(c==2)return 's';
    if(c==3)return 'p';
    return 'c';
}
void readin()
{
    int i,k;
    char c;
    memset(as,0,sizeof(as));
    for(i = 1; i <= 13; ++ i){
        scanf("%d%c",&k,&c);
        as[id(c)][k]++;
    }
}
bool judge1()
{
    int i,j;
    for(i = 1; i <= 4; ++ i)
    for(j = 1; j <= 9; ++ j)
    if(as[i][j]>4)return true;
    for(i = 1; i <= 3; ++ i)
        if(as[i][1]==0||as[i][9]==0)return false;
    for(i = 1; i <= 7; ++ i)
        if(as[4][i]==0)return false;
    for(i = 1; i <= 3; ++ i)
    for(j = 1; j <= 9; ++ j)
    if(as[i][j]&&(j!=1&&j!=9))return false;
    return true;
}
bool judge2()
{
    int i,j,k=0;
    for(i = 1; i <= 4; ++ i)
    for(j = 1; j <= 9; ++ j)
    if(as[i][j]==2)k++;
    return k==7;
}
bool dfs(int c,int cnt,int x)
{
    int i,f=0;
    if(c<=3){
        for(i = x; i <= 9; ++ i){
            if(as[c][i]>=3){
                f = 1;
                as[c][i]-=3;
                if(dfs(c,cnt+1,i)){
                    as[c][i] += 3;
                    return true;
                }
                as[c][i] += 3;
            }
            if(as[c][i]&&as[c][i+1]&&as[c][i+2]){
                f = 1;
                as[c][i]--;as[c][i+1]--;as[c][i+2]--;
                if(dfs(c,cnt+1,i)){
                    as[c][i]++;as[c][i+1]++;as[c][i+2]++;
                    return true;
                }
                as[c][i]++;as[c][i+1]++;as[c][i+2]++;
            }
        }
        if(!f){
            for(i = 1; i <= 9; ++ i)if(as[c][i])return false;
            return dfs(c+1,cnt,1);
        }
    }else{
        for(i = 1; i <= 7; ++  i){
            if(as[4][i]>=3){
                f = 1;
                as[4][i]-=3;
                if(dfs(c+1,cnt+1,i+1)){
                    as[4][i]+=3;
                    return true;
                }
                as[4][i]+=3;
            }
        }
        if(!f){
            for(i = 1; i <= 7; ++ i)if(as[4][i])return false;
            return true;
        }
    }
    return false;
}
bool judge()
{
    int i,j;
    if(judge2())return true;
    for(i = 1; i <= 3; ++ i){
        for(j = 1; j <= 9; ++ j)
            if(as[i][j]>=2){
                as[i][j]-=2;
                if(dfs(1,0,1)){
                    as[i][j]+=2;
                    return true;
                }
                as[i][j]+=2;
            }
    }
    for(i = 1; i <= 7; ++ i){
        if(as[4][i]>=2){
            as[4][i]-=2;
            if(dfs(1,0,1)){
                as[4][i]+=2;
                return true;
            }
            as[4][i]+=2;
        }
    }
    return false;
}
int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--){
        readin();
        bs.clear();

        if(0);
        else{
            for(i = 1; i <= 3; ++ i)
                for(j = 1; j <= 9; ++ j){
                    if(as[i][j]<=3){
                        as[i][j]++;
                        if(judge1()||judge())
                            bs.insert(make_pair(i,j));
                        as[i][j]--;
                    }
                }
            for(i = 1; i <= 7; ++ i){
                if(as[4][i]<3){
                    as[4][i]++;
                    if(judge1()||judge())
                        bs.insert(make_pair(4,i));
                    as[4][i]--;
                }
            }
        }

        if(bs.size()==0)puts("Nooten");
        else{
            pair<int,int> k;
            printf("%d",bs.size());
            for(p = bs.begin(); p != bs.end(); p++){
                k = *p;
                printf(" %d%c",k.second,fid(k.first));
            }
            puts("");
        }
    }

    return 0;
}

posted on 2012-10-26 18:17 aigoruan 阅读(...) 评论(...) 编辑 收藏

导航