HDU 4431 Mahjong (simulation + SEARCH)

Source: Internet
Author: User
Mahjong

Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)

Total submission (s): 2406 accepted submission (s): 500 problem descriptionjapanese Mahjong is a four-player game. the game needs four people to sit around a desk and play with a set of Mahjong tiles. A set of Mahjong tiles contains four copies of the tiles described next:


One to nine man, which we use 1 m to 9 m to represent;


One to nine sou, which we use 1 s to 9 s to represent;


One to nine pin, which we use 1 p to 9 p to represent;


Character tiles, which are: ton, Nan, SEI, Pei, Haku, hatsu, Chun, which we use 1C to 7c to represent.

A winning State means a set of 14 tiles that normally contains a pair of same tiles (which we call "eyes") and four melds. A meld is formed by either three same tiles (1 m, 1 m, 1 m or 2C, 2c, 2C for example) or three continuous non-character tiles (1 m, 2 m, 3 M or
5S, 6 s, 7 S for example ).

However, there are two special winning states that are different with the description above, which are:
"Chii toitsu", which means 7 different pairs of tiles;
"Kokushi Muso", which means a set of tiles that contains all these tiles: 1 m, 9 m, 1 P, 9 P, 1 s, 9 s and all 7 character tiles. and the rest tile shoshould also be one of the 13 tiles above.

And the game starts with four players starting ing 13 tiles. in each round every player must draw one tile from the deck one by one. if he reaches a winning state with these 14 tiles, he can say "Tsu Mo" and win the game. otherwise he shoshould discard one of his
14 tiles. And if the tile he throws out can form a winning state with the 13 tiles of any other player, the player can say "Ron" and win the game.

Now the question is, given the 13 tiles you have, does there exist any tiles that can form a winning state with your tiles?

(Notes: some of the pictures and descriptions above come from Wikipedia .)

Inputthe input data begins with a integer T (1 ≤ T ≤ 20000). Next are t cases, each of which contains 13 tiles. The description of every tile is as abve.

Outputfor each cases, if there actually exists some tiles that can form a winning state with the 13 tiles given, print the number first and then print all those tiles in order as the description order of tiles above. otherwise print a line
"Nooten" (without quotation marks ).

Sample Input

21s 2s 3s 2c 2c 2c 2p 3p 5m 6m 7m 1p 1p1p 1p 2p 3p 4s 5s 6s 7c 7c 3s 3s 2m 2m
 

Sample output

2 1p 4pNooten
 

Source2012 Asia Tianjin Regional Contest
Q: play mahjong, give you a card, and ask you what you can do with Hu. Note that the correct sum and thirteen are OK.
Idea: there are 13 cards on hand, and fourteen cards are added to the enumeration of possible cards. Then take an eyes pair, and the remaining 12 are all sentences (MELD) to satisfy the conditions.
Note: if there are more than three cards, there are several situations in which the cards form sentences.
Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <stack> # include <vector> # include <set> # include <queue> # define maxn 15 using namespace STD; int N, ANS, xxc, flag; int M [maxn], s [maxn], p [maxn], C [maxn]; int TM [maxn], TS [maxn], TP [maxn], TC [maxn]; int TTM [maxn], TTS [maxn], TTP [maxn], and TTC [maxn]; int HM [maxn], HS [maxn], HP [maxn], HC [maxn]; Cha R INS [maxn]; int State [maxn]; vector <int> AM, As, AP, AC; struct node {int ID; char op;} cur [maxn]; bool judge () {int I, j, CNT = 0; for (I = 1; I <= 9; I ++) {If (Tm [I] = 2) CNT ++; If (TS [I] = 2) CNT ++; If (TP [I] = 2) CNT ++; if (I <= 7 & TC [I] = 2) CNT ++;} If (CNT = 7) return true; for (I = 2; I <= 8; I ++) {If (Tm [I]) return false; If (TS [I]) return false; If (TP [I]) return false ;} if (Tm [1] & TM [9] & TS [1] & TS [9] & TP [1] & TP [9] & TC [1] & TC [2] & TC [3] & TC [4] & TC [5] & TC [6] & TC [7]) return true; return false;} bool valf (INT hem [], int HES [], int HEC [], int HEC []) {int I, J; for (I = 1; I <= 7; I ++) {While (hem [I] & hem [I + 1] & hem [I + 2]) hem [I] --, Hem [I + 1] --, Hem [I + 2] --; while (HES [I] & HES [I + 1] & HES [I + 2]) HES [I] --, hes [I + 1] --, hes [I + 2] --, [I + 1] --, [I + 2] --;} for (I = 1; I <= 9; I ++) {If (hem [I] | HES [I] | Heid [I]) return false; if (I <= 7 & HEC [I]) return false;} return true;} void DFS (INT POS) {If (FLAG) return; If (Pos = xxc + 1) {memcpy (HM, TTM, sizeof (TTM); memcpy (HS, TTS, sizeof (TTS); memcpy (HP, TTP, sizeof (TTP); memcpy (HC, TTC, sizeof (TTC); For (INT I = 1; I <= xxc; I ++) {If (State [I]) {int nid = cur [I]. ID; char NOP = cur [I]. OP; If (NOP = 'M') Hm [NID]-= 3; else if (NOP = 's') HS [NID]-= 3; else if (NOP = 'P') HP [NID]- = 3; else HC [NID]-= 3 ;}} if (valf (HM, HS, HP, HC) Flag = 1; return ;} state [POS] = 1; DFS (Pos + 1); State [POS] = 0; DFS (Pos + 1);} bool hehe () {int I, J; memcpy (TTM, TM, sizeof (TTM); memcpy (TTS, ts, sizeof (TTS); memcpy (TTP, TP, sizeof (TTP); memcpy (TTC, TC, sizeof (TTC); xxc = 0; for (I = 1; I <= 9; I ++) {If (TTM [I]> = 3) {xxc ++; cur [xxc]. id = I; cur [xxc]. OP = 'M';} If (TTS [I]> = 3) {xxc ++; cur [xxc]. id = I; cur [xxc]. OP ='s ';} If (TT P [I]> = 3) {xxc ++; cur [xxc]. id = I; cur [xxc]. OP = 'P';} if (I <= 7 & TTC [I]> = 3) TTC [I]-= 3;} If (xxc = 0) {If (valf (TTM, TTS, TTP, TTC) return true;} else {flag = 0; memset (State, 0, sizeof (State )); DFS (1); If (FLAG) return true;} return false;} bool work () {int I, j; If (Judge () return true; for (I = 1; I <= 9; I ++) // retrieves {If (Tm [I]> = 2) {TM [I]-= 2; if (hehe () {TM [I] + = 2; return true;} TM [I] + = 2 ;}} for (I = 1; I <= 9; I ++) // retrieves {If (TS [I]> = 2) {ts [I]-= 2; If (hehe ()) {ts [I] + = 2; return true;} ts [I] + = 2 ;}} for (I = 1; I <= 9; I ++) // retrieve {If (TP [I]> = 2) {TP [I]-= 2; If (hehe () {TP [I] + = 2; return true;} TP [I] + = 2;} for (I = 1; I <= 7; I ++) // retrieve {If (TC [I]> = 2) {TC [I]-= 2; If (hehe () {TC [I] + = 2; return true;} TC [I] + = 2;} return false;} void solve () {int I, j, T1, T2; memcpy (TS, S, sizeof (TS); memcpy (TP, P, sizeo F (TP); memcpy (TC, C, sizeof (TC); for (I = 1; I <= 9; I ++) {T1 = I-1; if (T1 = 0) T1 = 9; t2 = I + 1; if (t2 = 10) T2 = 1; if (! M [T1] &! M [I] &! M [T2]) continue; memcpy (TM, M, sizeof (TM); TM [I] ++; If (Tm [I]> 4) {TM [I] --; continue;} If (Work () {ans ++; AM. push_back (I);} TM [I] --;} memcpy (TM, M, sizeof (TM); memcpy (TP, P, sizeof (TP )); memcpy (TC, C, sizeof (TC); for (I = 1; I <= 9; I ++) {T1 = I-1; If (T1 = 0) t1 = 9; t2 = I + 1; if (t2 = 10) T2 = 1; if (! S [T1] &! S [I] &! S [T2]) continue; memcpy (TS, S, sizeof (TS); TS [I] ++; If (TS [I]> 4) {ts [I] --; continue;} If (Work () {ans ++;. push_back (I);} ts [I] --;} memcpy (TM, M, sizeof (TM); memcpy (TS, S, sizeof (TS )); memcpy (TC, C, sizeof (TC); for (I = 1; I <= 9; I ++) {T1 = I-1; If (T1 = 0) t1 = 9; t2 = I + 1; if (t2 = 10) T2 = 1; if (! P [T1] &! P [I] &! P [T2]) continue; memcpy (TP, P, sizeof (TP); TP [I] ++; If (TP [I]> 4) {TP [I] --; continue;} If (Work () {ans ++; AP. push_back (I);} TP [I] --;} memcpy (TM, M, sizeof (TM); memcpy (TS, S, sizeof (TS )); memcpy (TP, P, sizeof (TP); for (I = 1; I <= 7; I ++) {T1 = I-1; If (T1 = 0) t1 = 9; t2 = I + 1; if (t2 = 10) T2 = 1; if (! C [T1] &! C [I] &! C [T2]) continue; memcpy (TC, C, sizeof (TC); TC [I] ++; If (TC [I]> 4) {TC [I] --; continue;} If (Work () {ans ++; AC. push_back (I);} TC [I] --;} int main () {int I, j, T, CNT, SZ; scanf ("% d ", & T); While (t --) {memset (M, 0, sizeof (m); memset (S, 0, sizeof (s); memset (p, 0, sizeof (p); memset (C, 0, sizeof (c); AM. clear ();. clear (); AP. clear (); AC. clear (); for (I = 1; I <= 13; I ++) {scanf ("% s", INS ); if (INS [1] = 'M') m [INS [0]-'0'] ++; else if (INS [1] ='s ') s [INS [0]-'0'] ++; else if (INS [1] = 'P ') P [INS [0]-'0'] ++; else C [INS [0]-'0'] ++;} ans = 0; solve (); if (ANS = 0) printf ("nooten \ n"); else {printf ("% d", ANS); SZ = aM. size (); for (I = 0; I <SZ; I ++) {printf ("% DM", am [I]);} SZ =. size (); for (I = 0; I <SZ; I ++) {printf ("% ds", as [I]);} SZ = ap. size (); for (I = 0; I <SZ; I ++) {printf ("% DP", AP [I]);} SZ = ac. size (); for (I = 0; I <SZ; I ++) {printf ("% DC", AC [I]);} 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.