UVA

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("
");
    }
}

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