Sha 11210 Chinese Mahjong (brute force enumeration)

Source: Internet
Author: User

Sha 11210 Chinese Mahjong (brute force enumeration)
Sha 11210 Chinese Mahjong


Mahjong () is a game of Chinese Z? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys + CjxwPjxzdHJvbmc + keys =" re represented by 1 T, 2 T, 3 T, 4 T, 5 T, 6 T, 7 T, 8 T and 9 T.

Bams:Named as each tile (could t 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 1 S, 2 S, 3 S, 4S, 5S, 6 S, 7 S, 8 S and 9 S.

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 1 W, 2 W, 3 W, 4 W, 5 W, 6 W, 7 W, 8 W and 9 W.

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 always well off. the white tile (a clean board) means since you are now doing well you shoshould act like a good, inner upt 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? Counter 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.MeldIs 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 ). obviusly, 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 '".

InputThe 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 shoshould not be processed. outputFor each test case, print the case number and a list of waiting tiles sorted in the order appeared in the problem description (1 T ~ 9 T, 1 s ~ 9 S, 1 W ~ 9 W, DONG, NAN, XI, BEI, ZHONG, FA, BAI ). each waiting tile shoshould 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 FA1S 2S 3S 4S 5S 6S 7S 8S 9S 1T 3T 5T 7T0
Output for the Sample Input
Case 1: 1S 4S FACase 2: Not ready



Answer: Give 13 Mahjong cards and ask all the satisfied situations where a card can be used. There is no need to think too much about the cards here. You only need to satisfy the need to have one pair, while the others are all three plus or three identical ones. Some special cards do not need to be considered.

Solution: Convert all given Mahjong cards into numbers for processing, and use an array to calculate the number of cards. cnt [I] indicates that the mahjong cards labeled I have cnt [I] sheets. Because of the existence of special card faces, you can separate the labels of special cards. In this way, you do not need to consider whether consecutive labels are good or not during the dfs process. The next step is to enumerate all the cards that may be obtained (the cards cannot be retrieved again after four times), and then use the Backtracking Method to Determine whether cards can be used.


# Include
 
  
# Include
  
   
Int mj [20], cnt [35]; const char * mahjong [] = {"1 T", "2 T", "3 T", "4 T ", "5 T", "6 T", "7 T", "8 T", "9 T", "1 S", "2 S", "3 S ", "4 S", "5 S", "6 S", "7 S", "8 S", "9 S", "1 W", "2 W ", "3 W", "4 W", "5 W", "6 W", "7 W", "8 W", "9 W", "DONG ", "NAN", "XI", "BEI", "ZHONG", "FA", "BAI"}; int getNum (char * str) {// convert the card to the ID 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 ++) {// engraved subif (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 ++) {// subif (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) {// convert 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 ); // convert the card to number} int flag = 0; for (int I = 0; I <34; I ++) {// Memset (cnt, 0, sizeof (cnt); for (int j = 0; j <13; j ++) {cnt [mj [j] ++; // count the number of occurrences of each card} if (cnt [I]> = 4) continue; // This card cnt [I] ++ will not be taken into consideration if (check () {flag = 1; printf ("% s ", mahjong [I]) ;}} if (! Flag) printf ("Not ready \ n"); else printf ("\ n");} return 0 ;}
  
 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.