UVA - 11210 Chinese Mahjong

link

 

Input

The input consists of at most 50 test cases. Each case consists of 13 tiles in a single line. The hand is
legal (e.g. no invalid tiles, exactly 13 tiles). The last case is followed by a single zero, which should not
be processed.
Output
For each test case, print the case number and a list of waiting tiles sorted in the order appeared in
the problem description (1T~9T, 1S~9S, 1W~9W, DONG, NAN, XI, BEI, ZHONG, FA, BAI). Each waiting tile
should be appeared exactly once. If the hand is not ready, print a message `Not ready' without quotes.
Sample Input
1S 1S 2S 2S 2S 3S 3S 3S 7S 8S 9S FA FA
1S 2S 3S 4S 5S 6S 7S 8S 9S 1T 3T 5T 7T
0
Sample Output
Case 1: 1S 4S FA
Case 2: Not ready

 

#include <cstdio>
#include <cstring>

using namespace std;

int c[34];
const char *ma[] = {"1T", "2T", "3T", "4T", "5T", "6T", "7T", "8T", "9T", "1S", "2S", "3S", "4S", "5S", "6S", "7S",
                    "8S", "9S", "1W", "2W", "3W", "4W", "5W", "6W", "7W", "8W", "9W", "DONG", "NAN", "XI", "BEI",
                    "ZHONG", "FA", "BAI"};

int convert(char *s);

bool check(int i);

int main() {
    char s[100];
    for (int base = 1; scanf("%s", s);) {
        bool ok = false;
        if (s[0] == '0')
            break;
        printf("Case %d:", base++);
        memset(c, 0, sizeof(c));
        c[convert(s)]++;
        for (int i = 1; i < 13; ++i) {
            scanf("%s", s);
            c[convert(s)]++;
        }

        for (int i = 0; i < 34; ++i) {
            if (c[i] >= 4)
                continue;
            c[i]++;
            for (int j = 0, p = 0; j < 34; ++j) { //找将
                if (c[j] >= 2) {
                    c[j] -= 2;
                    p = check(0);
                    c[j] += 2;
                    if (p) {
                        ok = 1;
                        printf(" %s", ma[i]);
                        break;
                    }
                }
            }
            c[i]--;
        }
        if (!ok)
            printf(" Not ready");
        printf("\n");
    }
}

bool check(int h) {
    if (h == 4)
        return true;
    for (int i = 0; i < 34; ++i) { //刻子
        if (c[i] >= 3) {
            c[i] -= 3;
            bool p = check(h + 1);
            c[i] += 3;
            if (p) return true;
        }
        if (i < 27 and i % 9 < 7 and c[i] > 0 and c[i + 1] > 0 and c[i + 2] > 0) {//顺子
            for (int t = 0; t < 3; ++t) c[i + t]--;
            bool p = check(h + 1);
            for (int t = 0; t < 3; ++t) c[i + t]++;
            if (p) return true;
        }
    }
    return false;
}

int convert(char *s) {
    for (int i = 0; i < 34; ++i) {
        if (!strcmp(s, ma[i]))
            return i;
    }
    return -1;
}

 

转载于:https://www.cnblogs.com/wangsong/p/7529210.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值