Mahjong () is a game of Chinese Z喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcmlnaW4gdXN1YWxseSBwbGF5ZWQgYnkgZm91ciBwZXJzb25zIHdpdGggdGlsZXMgcmVzZW1ibGluZyBkb21pbm9lcyBhbmQgYmVhcmluZyB2YXJpb3VzIGRlc2lnbnMsIHdoaWNoIGFyZSBkcmF3biBhbmQgZGlzY2FyZGVkIHVudGlsIG9uZSBwbGF5ZXIgd2lucyB3aXRoCiBhIGhhbmQgb2YgZm91ciBjb21iaW5hdGlvbnMgb2YgdGhyZWUgdGlsZXMgZWFjaCBhbmQgYSBwYWlyIG9mIG1hdGNoaW5nIHRpbGVzLjwvcD4KPHA+QSBzZXQgb2YgTWFoam9uZyB0aWxlcyB3aWxsIHVzdWFsbHkgZGlmZmVyIGZyb20gcGxhY2UgdG8gcGxhY2UuIEl0IHVzdWFsbHkgaGFzIGF0IGxlYXN0IDEzNiB0aWxlcywgbW9zdCBjb21tb25seSAxNDQsIGFsdGhvdWdoIHNldHMgb3JpZ2luYXRpbmcgZnJvbSBBbWVyaWNhIG9yIEphcGFuIHdpbGwgaGF2ZSBtb3JlLiBUaGUgMTM2LXRpbGUgTWFoam9uZyBpbmNsdWRlczo8L3A+CjxwPjxzdHJvbmc+RG90czo8L3N0cm9uZz4gbmFtZWQgYXMgZWFjaCB0aWxlIGNvbnNpc3RzIG9mIGEgbnVtYmVyIG9mIGNpcmNsZXMuIEVhY2ggY2lyY2xlIGlzIHNhaWQgdG8gcmVwcmVzZW50IGNvcHBlciAodG9uZykgY29pbnMgd2l0aCBhIHNxdWFyZSBob2xlIGluIHRoZSBtaWRkbGUuIEluIHRoaXMgcHJvYmxlbSwgdGhleQ=="re represented by 1T, 2T, 3T, 4T, 5T, 6T, 7T, 8T and 9T.
Bams: named as each tile (except the 1 Bamboo) consists of a number of bamboo sticks. Each stick is said to represent a string (suo) that holds a hundred coins. In this problem, they"re represented by 1S, 2S, 3S, 4S, 5S, 6S, 7S, 8S and 9S.
Craks: named as each tile represents ten thousand (wan) coins, or one hundred strings of one hundred coins. In this problem, they"re represented by 1W, 2W, 3W, 4W, 5W, 6W, 7W, 8W and 9W.
Wind tiles: East, South, West, and North. In this problem, they"re represented by DONG, NAN, XI, BEI.
Dragon tiles: red, green, and white. The term dragon tile is a western convention introduced by Joseph Park Babcock in his 1920 book introducing Mahjong to America. Originally, these tiles are said to have something to do with the Chinese Imperial Examination. The red tile means you pass the examination and thus will be appointed a government official. The green tile means, consequently you will become financially well off. The white tile (a clean board) means since you are now doing well you should act like a good, incorrupt official. In this problem, they"re represented by ZHONG, FA, BAI.
There are 9*3+4+3=34 kinds, with exactly 4 tiles of each kind, so there are 136 tiles in total.
To who may be interested, the 144-tile Mahjong also includes:
Flower tiles:
typically optional components to a set of mahjong tiles, often contain artwork on their tiles. There are exactly one tile of each kind, so 136+8=144 tiles in total. In this problem, we don?ˉt consider these tiles.Chinese Mahjong is very complicated. However, we only need to know very few of the rules in order to solve this problem. A meld is a certain set of tiles in one"s hand. There are three kinds of melds you need to know (to who knows Mahjong already, kong is not considered):
Pong: A set of three identical titles. Example: ;.
Chow: A set of three suited tiles in sequence. All three tiles must be of the same suites. Sequences of higher length are not permissible (unless it forms more than one meld). Obviously, wind tiles and dragon tiles can never be involved in chows. Example:;.
Eye: The pair, while not a meld, is the final component to the standard hand. It consists of any two identical tiles.
A player wins the round by creating a standard mahjong hand. That means, the hand consists of an eye and several (possible zero) pongs and chows. Note that each 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 '".
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
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; }