UVA 11210 (14.4.15)(二)

2014-11-24 11:56:03 · 作者: · 浏览: 1
h title can be involved in exactly one eye/pong/chow.

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

做题心得:这是大白书上的例题, 看完完整的思路和代码后 重新敲的, 事先是觉得毫无头绪, 看完再敲一遍觉得真的是思路理清了再去做速度很快.


思路:枚举每一张麻将牌, 看是否可以胡, 在这个枚举之下还有嵌套枚举, 就是加入第i个麻将后, 枚举手上14张牌中怎么选对子, 每个对子选完后, 就去递归查询是否剩下的牌都是ABC型 或是 AAA型就好了


AC代码 有注释:

#include
   
    
#include
    
      int has[35]; const char * majiang[] = {"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数值表示 int convert(char *s) { for(int i = 0; i < 34; i++) { if(strcmp(s, majiang[i]) == 0) return i; } return -1; } //找三张相同牌或者三张顺子牌 int search(int num) { int i; //找三张相同 for(i = 0; i < 34; i++) { if(has[i] >= 3) { if(num == 3) return 1; has[i] -= 3; if(search(num+1)) return 1; has[i] += 3; } } //找顺子, 注意风牌箭牌不能拼顺子 for(i = 0; i <= 24; i++) { if(i % 9 < 7 && has[i] >= 1 && has[i+1] >= 1 && has[i+2] >= 1) { if(num == 3) return 1; has[i]--; has[i+1]--; has[i+2]--; if(search(num+1)) return 1; has[i]++; has[i+1]++; has[i+2]++; } } return 0; } int check() { for(int i = 0 ; i < 34; i++) { if(has[i] >= 2) { has[i] -= 2; if(search(0)) return 1; has[i] += 2; } } return 0; } int main() { int cas = 0; int ok; char str[100]; int mj[15]; while(scanf("%s", str) == 1) { int i, j; if(str[0] == '0') break; mj[0] = convert(str); for(i = 1; i < 13; i++) { scanf("%s", str); mj[i] = convert(str); } printf("Case %d:", ++cas); ok = 0; //针对每个麻将枚举 for(i = 0; i < 34; i++) { memset(has, 0, sizeof(has)); for(j = 0; j < 13; j++) has[mj[j]]++; //该牌最多4张, 如果当前有4张了, 那么明显i麻将是不能加入的, 故continue; if(has[i] >= 4) continue; has[i]++; if(check()) { ok = 1; printf(" %s", majiang[i]); } has[i]--; } if(ok == 0) printf(" Not ready"); printf("\n"); } return 0; }