设为首页 加入收藏

TOP

uva 11210 Chinese Mahjong(暴力枚举)(二)
2015-07-20 17:23:26 来源: 作者: 【 】 浏览:5
Tags:uva 11210 Chinese Mahjong 暴力 枚举
e 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



题目大意:给出13张麻将牌,问在取一张牌就可以胡牌的牌,所处所有满足的情况。这里的胡牌不需要考虑太多,只需要满足存在一个对子, 而其他的全是3个顺或者3个相同的就可以了,一些特殊的胡牌不需要考虑。

解题思路:将所有给出的麻将牌转化成数字进行处理,对应的用一个数组统计牌的个数cnt[i]表示标号为i的麻将牌有cnt[i]张。因为存在特殊的牌面,所以可以将特殊牌面的标号分开,这样在dfs的过程中就无需考虑连续的标号是否是顺子的问题。接下来就是枚举所有可能拿到的牌(出现过4次的不能再取),加入后用回溯法判断是否可以胡牌。


#include
  
   
#include
   
     int mj[20], cnt[35]; const char* mahjong[]={"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 getNum(char *str) { //将牌面转换为编号 for (int i = 0; i < 34; i++) { if (!strcmp(mahjong[i], str)) return i; } } int check2(int n) { for (int i = 0; i < 34; i++) { //刻子 if (cnt[i] >= 3) { if (n == 3) return 1; cnt[i] -= 3; if (check2(n + 1)) return 1; cnt[i] += 3; } } for (int i = 0; i <= 24; i++) { //顺子 if (i % 9 <= 6 && cnt[i] && cnt[i + 1] && cnt[i + 2]) { if (n == 3) return 1; cnt[i]--; cnt[i + 1]--; cnt[i + 2]--; if (check2(n + 1)) return 1; cnt[i]++; cnt[i + 1]++; cnt[i + 2]++; } } return 0; } int check() { for (int i = 0; i < 34; i++) { if (cnt[i] >= 2) {//将 cnt[i] -= 2; if (check2(0)) return 1; cnt[i] += 2; } } return 0; } int main() { char str[3]; int Case = 1; while (scanf("%s", str) == 1) { if (strcmp(str, "0") == 0) break; printf("Case %d:", Case++); mj[0] = getNum(str); for (int i = 1; i < 13; i++) { scanf("%s", str); mj[i] = getNum(str);//将牌面转化为编号 } int flag = 0; for (int i = 0; i < 34; i++) {//枚举34种可能和的情况 memset(cnt, 0, sizeof(cnt)); for (int j = 0; j < 13; j++) { cnt[mj[j]]++;//统计每张牌出现次数 } if (cnt[i] >= 4) continue;//已出现四次则不再考虑这张牌 cnt[i]++; if (check()) { flag = 1; printf(" %s", mahjong[i]); } } if (!flag) printf(" Not ready\n"); else printf("\n"); } return 0; }
   
  





首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ZOJ 2969 && BNU16488 Ea.. 下一篇UVA 10534--Wavio Sequence+二分+..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python爬虫教程(从 (2025-12-26 16:49:14)
·【全269集】B站最详 (2025-12-26 16:49:11)
·Python爬虫详解:原 (2025-12-26 16:49:09)
·Spring Boot Java: (2025-12-26 16:20:19)
·Spring BootでHello (2025-12-26 16:20:15)