uva 11210 - Chinese Mahjong 暴力回溯

简介:

   最近很颓废,一直不想写题,今天说写题结果一直拖到现在才写了一题……

   一个很简单的模拟题,只要会打麻将就会写,注意牌不能超过4张的,编号的转换有点累



/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int num[40];
int s2i(char *s)
{
    switch(s[0])
    {
        case 'D':return 1;
        case 'N':return 2;
        case 'X':return 3;
        case 'B':if(s[1]=='E')return 4;
                 else return 7;
        case 'Z':return 5;
        case 'F':return 6;
        default:
        switch(s[1])
        {
            case 'T':return s[0]-'0'+10;
            case 'S':return s[0]-'0'+20;
            case 'W':return s[0]-'0'+30;
            default:return 0;
        }
    }
}
string ss[10]={"DONG","NAN","XI","BEI","ZHONG","FA","BAI"};
string oo[10]={"0","1","2","3","4","5","6","7","8","9"};
string i2s(int now)
{
    if(now<10)return ss[now-1];
    switch(now/10)
    {
        case 1:return oo[now%10]+"T";
        case 2:return oo[now%10]+"S";
        case 3:return oo[now%10]+"W";
        default:return "error";
    }
}
bool win(int two,int three,int i)
{
    if(i==40)return 0;
    if(num[i]==0)return win(two,three,i+1);
    if(two&&num[i]>=2)
    {
        if(!three)return 1;
        num[i]-=2;
        if(win(0,three,i)){num[i]+=2;return 1;}
        num[i]+=2;
    }
    if(three&&num[i]>=3)
    {
        if(three==1&&!two)return 1;
        num[i]-=3;
        if(win(two,three-1,i)){num[i]+=3;return 1;}
        num[i]+=3;
    }
    if(three&&num[i]&&i>10)
    {
        for(int t=-2;t<=0;t++)
        {
            if((i+t)/10!=(i+t+2)/10)continue;
            if(!num[i+t]||!num[i+t+1]||!num[i+t+2])continue;
            if(three==1&&!two)return 1;
            num[i+t]--;num[i+t+1]--;num[i+t+2]--;
            if(win(two,three-1,i)){num[i+t]++;num[i+t+1]++;num[i+t+2]++;return 1;}
            num[i+t]++;num[i+t+1]++;num[i+t+2]++;
        }
    }
    return 0;
}
void solve(int T)
{
    cout<<"Case "<<T<<":";
    bool flag=1;
    for(int i=11;i!=10;i++)//因为顺序要求
    {
        if(!(i%10)||num[i]==4)continue;
        num[i]++;
        if(win(1,4,1)){cout<<" "<<i2s(i);flag=0;}
        num[i]--;
        if(i==39)i=0;
    }
    if(flag)cout<<" Not ready";
    cout<<endl;
}
bool input()
{
    char s[100];
    int i=0;
    memset(num,0,sizeof(num));
    do
    {
        i++;
        scanf("%s",s);
        if(s[0]=='0')return 0;
        num[s2i(s)]++;
    }while(i<13);
    return 1;
}
int main()
{
    int T=0;
    while(input())solve(++T);
}

/*
4T 5T 6T 6T 6T 6T 7T 8T 9T 9T 9T BAI BAI
4T 5T 6T 6T 6T 6T 7T 8T 9T 9T 9T 1W 1W
2T 3T 3T 3T 4T 4T 4T 5T 5T 6T 6T 7T 7T
1T 1T 2T 2T 2T 3T 3T 3T 4T 4T 5T 5T 5T
1T 1T 1T 2T 2T 3T 3T 3T 4T 4T 5T 5T 5T
1T 1T 1T 2T 2T 3T 3T 3T 4T 4T 5T 5T 6T
1T 1T 1T 2T 2T 3T 3T 3T 4T 4T 5T 6T 6T
1T 1T 1T 2T 2T 3T 3T 3T 4T 4T 6T 6T 6T
1T 1T 1T 2T 2T 3T 3T 3T 4T 5T 6T 6T 6T
1T 1T 1T 2T 2T 3T 3T 4T 4T 5T 6T 6T 6T
1T 1T 2T 2T 2T 3T 3T 4T 4T 5T 6T 6T 6T
1T 1T 2T 2T 3T 3T 3T 4T 4T 5T 6T 6T 6T
1T 1T 2T 2T 3T 3T 4T 4T 4T 5T 6T 6T 6T
1T 1T 2T 2T 3T 4T 4T 4T 4T 5T 6T 6T 6T
1T 2T 2T 2T 2T 3T 4T 4T 4T 5T 6T 6T 6T
2T 2T 2T 2T 3T 3T 4T 4T 4T 5T 6T 6T 6T
2T 2T 2T 2T 3T 3T 4T 4T 4T 5T 5T 6T 6T
1T 2T 2T 2T 3T 3T 4T 4T 4T 5T 6T 6T 6T
1T 1T 2T 2T 2T 3T 4T 4T 4T 5T 6T 6T 6T
1T 2T 2T 2T 3T 4T 4T 4T 5T 6T 6T 6T 7T
0

Case 1: 3T 9T BAI
Case 2: 3T 9T 1W
Case 3: 1T 2T 3T 4T 6T 7T
Case 4: 1T 3T 4T 6T
Case 5: 1T 2T 3T 4T 5T
Case 6: 1T 2T 3T 4T 6T 7T
Case 7: 1T 4T 5T 6T
Case 8: 2T 3T 4T 5T
Case 9: 1T 2T 3T 4T 6T
Case 10: 1T 2T 3T 4T 5T 6T 7T
Case 11: 1T 2T 3T 5T
Case 12: 1T 2T 4T 5T
Case 13: 1T 3T 4T 5T 6T 7T
Case 14: 1T 2T 3T
Case 15: 3T 4T 5T 6T 7T
Case 16: 1T 3T 4T 5T 6T
Case 17: 1T 3T 4T 6T 7T
Case 18: 2T 3T 4T
Case 19: 1T 4T
Case 20: 2T 6T
*/


目录
相关文章
|
8月前
【每日一题Day130】LC1255得分最高的单词集合 | 回溯
【每日一题Day130】LC1255得分最高的单词集合 | 回溯
52 0
|
8月前
【每日一题Day331】LC2560打家劫舍 IV | 二分查找 + 贪心
【每日一题Day331】LC2560打家劫舍 IV | 二分查找 + 贪心
47 0
|
7月前
【洛谷 P1219】[USACO1.5]八皇后 Checker Challenge 题解(深度优先搜索+回溯法)
**USACO1.5八皇后挑战**是关于在$n\times n$棋盘上放置棋子的,确保每行、每列及两条主对角线上各有一个且仅有一个棋子。给定$6$作为输入,输出前$3$个解及解的总数。例如,对于$6\times6$棋盘,正确输出应包括解的序列和总数。代码使用DFS解决,通过跟踪对角线占用状态避免冲突。当找到所有解时,显示前三个并计数。样例输入$6$产生输出为解的前三个排列和总数$4$。
44 0
|
8月前
|
计算机视觉
【每日一题Day231】LC1240铺瓷砖 | 暴力回溯
【每日一题Day231】LC1240铺瓷砖 | 暴力回溯
68 0
UVa668 - Parliament(贪心)
UVa668 - Parliament(贪心)
66 0
|
文件存储
Easy Number Challenge(埃式筛思想+优雅暴力)
Easy Number Challenge(埃式筛思想+优雅暴力)
89 0
【LeetCode 热题 HOT 100 中等】46. 全排列(回溯)
【LeetCode 热题 HOT 100 中等】46. 全排列(回溯)
【LeetCode 热题 HOT 100 中等】46. 全排列(回溯)

热门文章

最新文章