Question link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4431
Question:
Give a deck and find all the cards that can be pasted.
Solution:
Enumerate each card to see if it can be pasted.
Because there are only 14 cards in total, each time the basis will be removed from the three cards, to determine whether the last two cards are the same.
Seven-to-thirteen-to-one separate consideration,
Note:
1, 1 P 1 P 1 s 9 S 1 m 9 m 1C 2C 3C 4C 5C 6C 7C such can also paste 9 p.
2. For the 7-to-7 pairs, you must use different cards. For the 1 s, 1 s, 1 s, and 1 s, you cannot use the 7-to-7 pairs.
Pruning:
1. For cards that are not c-colored, cards that can be pasted must appear in the cards of the hand or + 1,-1 and the same color. Therefore, you only need to enumerate the cards that can be pasted.
2. For a separate card, when there is no same color card between the left and right sides, you cannot paste it.
Paste several groups of test data:
20
1 P 2 P 3 P 7 p 8 p 9 P 1 S 2 S 3 s 1C 1C 2C
2 1C 2C
1 P 2 P 3 P 3 P 3 P 4 P 5 P 1 M 2 m 3 M 4 M 5 m
2 3 M 6 m
1 S 2 S 3 s 2C 2C 2 P 3 P 5 M 6 M 7 m 1 P 1 p
2 1 P 4 P
1 p 1 P 2 P 3 P 4S 5S 6 s 7C 7C 3 S 3 S 2 M 2 m
Nooten
1 p 1 P 1 P 2 P 2 P 3 P 4 P 4 P 5 p 6 p 7 p 8 p
6 2 P 3 P 5 p 6 p 8 p 9 p
1 P 2 P 3 P 3 P 4 P 5 P 1 M 2 m 3 M 4 M 5 M 6 m
2 3 P 6 p
1 P 2 P 3 P 3 P 3 P 4 P 5 P 1 M 2 m 3 M 4 M 5 m
2 3 M 6 m
1 P 2 P 3 P 3 P 3 P 4 P 5 p 6 P 1 M 2 m 3 M 4 m
2 1 M 4 m
1c 2C 3C 4C 5C 6C 7C 1 p 9 P 1 m 9 M 1 s 9 s
13 1 m 9 M 1 s 9 S 1 p 9 p 1C 2C 3C 4C 5C 6C 7c
1 p 1 P 3 P 2 P 5 P 2 P 6 p 7 P 3 P 4 P 5 p 6 P 4 P
3 1 P 4 p 7 P
1 p 1 P 2 P 3 P 2 P 3 P 4 P 4 P 5 P 5 p 6 p 7 p 7 P
2 3 P 6 p
1 p 1 p 9 p 9 P 1 M 1 m 9 m 9 M 1 s 9 S 1 s 9 s 7c
1 7c
1 p 1 P 1 P 1 P 1 M 1 M 1 M 1 S 1 s 9 s 1C 1c 1 s 9 s
Nooten
1 p 1 P 1 s 9 S 1 m 9 m 1C 2C 3C 4C 5C 6C 7c
1 9 p
Code:
# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define EPS 1e-6 # define INF 0x1f1f1f1f # define PI ACOs (-1.0) # define ll _ int64 # define lson L, M, (RT <1) # define rson m + 1, R, (RT <1) | 1 // # pragma comment (linker, "/Stack: 1024000000,1024000000") using names Pace STD;/* freopen ("data. in "," r ", stdin); freopen (" data. out "," W ", stdout); */Map <char, int> myp; int save [5] [15], CNT; char SA [5]; char PP [4] = {'M', 's', 'P', 'C'}; bool flag, ANS [40], hav [40]; void juqi (int A) // judge whether it is a seven pair {for (INT I = 0; I <34; I ++) {int x = I/9, y = I % 9; If (! Save [x] [Y]) continue; If (save [x] [Y]! = 2) return;} // printf ("qidui Yes \ n"); ++ CNT, ANS [a] = true; return;} void jushi (int) // determine whether it is thirteen. {for (INT I = 0; I <3; I ++) {If (save [I] [0]! = 1 & save [I] [0]! = 2) return; If (save [I] [8]! = 1 & save [I] [8]! = 2) return; For (Int J = 1; j <8; j ++) if (save [I] [J]) return ;}for (INT I = 0; I <7; I ++) if (save [3] [I]! = 1 & save [3] [I]! = 2) return; ++ CNT, ANS [a] = true; return;} void DFS (INT num, int Val) {If (FLAG) return; if (num = 2) {for (INT I = 0; I <4; I ++) {for (Int J = 0; j <= (I = 3 )? ); J ++) if (save [I] [J] = 2) // The last two cards must be the same to paste {flag = true; if (! Ans [Val]) ++ CNT, ANS [Val] = true; return;} else if (save [I] [J]) return ;}} for (INT I = 0; I <4; I ++) {for (Int J = 0; j <(I = 3? ); J ++) {If (save [I] [J]> = 3) // three identical cards {save [I] [J]-= 3; DFS (num-3, Val); save [I] [J] + = 3; if (FLAG) return;} If (! J | j = 8 | I = 3) continue; if (save [I] [J] & save [I] [J-1] & save [I] [J + 1]) // three identical color cards in a row, note that the C card does not work {save [I] [J] --, save [I] [J-1] --, save [I] [J + 1] --; DFS (num-3, val); save [I] [J] ++, save [I] [J-1] ++, save [I] [J + 1] ++; If (FLAG) return ;}}} bool iscan () {for (INT I = 0; I <3; I ++) {If (save [I] [0] = 1 & save [I] [1] = 0) return false; Int J; For (j = 1; j <= 7; j ++) // when there is only one card, and left and right no card {If (save [I] [J] = 1 & save [I] [J-1] = 0 & save [I] [J + 1] = = 0) return F Alse;} If (save [I] [J] = 1 & save [I] [J-1] = 0) return false;} For (INT I = 0; I <7; I ++) if (save [3] [I] = 1) return false; return true;} int main () {myp ['M'] = 0, myp ['s'] = 1, myp ['P'] = 2, myp ['C'] = 3; int T; scanf ("% d", & T); While (t --) {memset (save, 0, sizeof (SAVE); memset (ANS, false, sizeof (ANS); memset (HAV, false, sizeof (HAV); For (INT I = 1; I <= 13; I ++) {scanf ("% s", SA); save [myp [SA [1] [* Sa-'0'-1] ++; int xx = myp [SA [1] * 9 + * Sa-'0'-1; hav [XX] = true; // hav is a brand that can be pasted if (XX % 9) // Of course C card some can not paste into it, it does not matter {If (XX % 9 = 8) hav [xx-1] = true; else hav [xx-1] = hav [xx + 1] = true;} else hav [xx + 1] = true;} CNT = 0; For (INT I = 0; I <34; I ++) {If (save [I/9] [I % 9]> = 4) // this card has no continue; save [I/9] [I % 9] ++; jushi (I); If (! Hav [I] |! Iscan () // two pruning methods {save [I/9] [I % 9] --; continue;} juqi (I); flag = false; DFS (14, i); save [I/9] [I % 9] --;} If (! CNT) {printf ("nooten \ n"); continue;} printf ("% d", CNT); For (INT I = 0; I <34; I ++) {If (ANS [I]) printf ("% d % C", I % 9 + 1, PP [I/9]);} putchar ('\ n');} return 0 ;}