题目
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2151
题意
麻将,有13张牌,问再加哪一张牌可以凑成一对,若干个三张和若干个三联。
思路
如刘书思路,明显,这是一道模拟题。
感想
1. 三倍ice cream!
代码
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <tuple> #include <set> #include <map> #include <cassert> #define LOCAL_DEBUG using namespace std; string cardsName[] = { "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 cards[14]; bool used[14]; int cardCaches[14]; int cnt[34]; bool read(map<string, int> cardsMap) { memset(cnt, 0, sizeof cnt); for (int i = 0; i < 13; i++) { char tmp[6]; scanf("%s", tmp); if (tmp[0] == '0')return false; cards[i] = cardsMap[tmp]; cnt[cards[i]] ++; } return true; } bool subcheck(int sid) { if (sid > 13)return true; if (used[sid])return subcheck(sid + 1); int card = cardCaches[sid]; if (cnt[card] >= 3) { used[sid] = used[sid + 1] = used[sid + 2] = true; cnt[card] -= 3; bool fl = subcheck(sid + 3); cnt[card] += 3; used[sid] = used[sid + 1] = used[sid + 2] = false; if (fl)return true; } if (card < 27 && card % 9 < 7 && cnt[card] >0 && cnt[card + 1] > 0 && cnt[card + 2] > 0) { int tmp[3] = { -1, -1, -1 }; for (int i = sid; i < 14; i++) { if (used[i])continue; int ind = cardCaches[i] - cardCaches[sid]; if (tmp[ind] == -1) { tmp[ind] = i; } if (ind == 2)break; } for (int ind = 0; ind < 3; ind++) { cnt[cardCaches[sid] + ind] --; used[tmp[ind]] = true; } bool fl = subcheck(sid + 1); for (int ind = 0; ind < 3; ind++) { cnt[cardCaches[sid] + ind] ++; used[tmp[ind]] = false; } if (fl)return true; } return false; } bool check() { memset(used, 0, sizeof used); for (int i = 0; i < 14; i++) { if (i && cardCaches[i] == cardCaches[i - 1])continue; if (cnt[cardCaches[i]] >= 2) { used[i] = used[i + 1] = true; cnt[cardCaches[i]] -= 2; bool fl = subcheck(0); used[i] = used[i + 1] = false; cnt[cardCaches[i]] += 2; if(fl)return true; } } return false; } int main() { #ifdef LOCAL_DEBUG freopen("input.txt", "r", stdin); //freopen("output2.txt", "w", stdout); #endif // LOCAL_DEBUG map<string, int> cardsMap; for (int i = 0; i < 34; i++) { cardsMap[cardsName[i]] = i; } for (int ti = 1; read(cardsMap); ti++) { string ans; for (int i = 0; i < 34; i++) { if (cnt[i] == 4)continue; cards[13] = i; cnt[i]++; for (int j = 0; j < 14; j++)cardCaches[j] = cards[j]; sort(cardCaches, cardCaches + 14); if (check()) { ans += " "; ans += cardsName[i]; } cnt[i]--; } if (ans.length() == 0) { printf("Case %d: Not ready ", ti); } else { printf("Case %d:%s ", ti, ans.c_str()); } } return 0; }