When a hand is one tile short of wining, the hand is said to be a ready hand, or more figuratively, "on the pot'. The player holding a ready hand is said to be waiting for certain tiles. For example

is waiting for
,
and
.
To who knows more about Mahjong: don"t consider special winning hands such as '
".
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
Output for the Sample Input
Case 1: 1S 4S FA Case 2: Not ready
题意:麻将,给一副牌,问哪几张牌可以听。
思路:暴力枚举。。
代码:
#include#include const char *mj[] = { "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 c[34], C[13]; char s[100]; int find(char *s) { for (int i = 0; i < 34; i ++) if (strcmp(mj[i], s) == 0) return i; return -1; } void init() { C[0] = find(s); for (int i = 1; i < 13; i ++) { scanf("%s", s); C[i] = find(s); } } bool dfs(int d) { if (d == 4) return true; for (int i = 0; i < 34; i ++) { if (c[i] >= 3) { c[i] -= 3; if (dfs(d + 1)) return true; c[i] += 3; } } for (int i = 0; i <= 24; i++) { if (i % 9 <= 6 && c[i] >= 1 && c[i + 1] >= 1 && c[i + 2] >= 1) { c[i]--; c[i + 1]--; c[i + 2]--; if (dfs(d + 1)) return true; c[i] ++; c[i + 1]++; c[i + 2]++; } } return false; } bool judge() { for (int i = 0; i < 34; i ++) { if (c[i] < 2) continue; c[i] -= 2; if (dfs(0)) return true; c[i] += 2; } return false; } void solve() { int flag = 0; for (int i = 0; i < 34; i ++) { memset(c, 0, sizeof(c)); for (int j = 0; j < 13; j ++) c[C[j]]++; if (c[i] >= 4) continue; c[i] ++; if (judge()) { flag = 1; printf(" %s", mj[i]); } c[i] --; } if (flag) printf("\n"); else printf(" Not ready\n"); } int main() { int cas = 0; while (~scanf("%s", s) && s[0] != '0') { init(); printf("Case %d:", ++cas); solve(); } return 0; }