這道題在數據結構這一章裏算是簡單的,但是很容易WA和RE,可能得debug很長時間。
值得注意的地方:
1.紙牌被處理的次數中,每次發牌並且檢查一次匹配,算處理一次,而不是四次
2.查看匹配的時候,如果一次匹配成功並且刪除了三張紙牌,要繼續檢查是否匹配,這是個循環的過程
3.可以通過用set記錄State判重,判斷打平的情況
4.紙牌收回手中時,注意紙牌擺放的順序
#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
deque<int> hand;
vector<deque<int>> tab;
set<vector<deque<int>>> s;
bool input(){
cnt = 0; hand.clear();
while (true){
int x; cin >> x;
if (!x) return false;
hand.push_back(x);
if (hand.size() == 52) return true;
}
}
bool can(int a, int b, int c, deque<int> & cur){
int val1 = a >= 0 ? *(cur.begin() + a) : *(cur.end() + a);
int val2 = b >= 0 ? *(cur.begin() + b) : *(cur.end() + b);
int val3 = c >= 0 ? *(cur.begin() + c) : *(cur.end() + c);
int sum = val1 + val2 + val3;
return sum == 10 || sum == 20 || sum == 30;
}
bool match(deque<int> & cur){
if (can(0, 1, -1, cur)){
int t0 = cur.front(); cur.pop_front();
int t1 = cur.front(); cur.pop_front();
int t_1 = cur.back(); cur.pop_back();
hand.push_back(t0); hand.push_back(t1); hand.push_back(t_1);
return true;
}
if (can(0, -1, -2, cur)){
int t0 = cur.front(); cur.pop_front();
int t_1 = cur.back(); cur.pop_back();
int t_2 = cur.back(); cur.pop_back();
hand.push_back(t0); hand.push_back(t_2); hand.push_back(t_1);
return true;
}
if (can(-1, -2, -3, cur)){
int t_1 = cur.back(); cur.pop_back();
int t_2 = cur.back(); cur.pop_back();
int t_3 = cur.back(); cur.pop_back();
hand.push_back(t_3); hand.push_back(t_2); hand.push_back(t_1);
return true;
}
}
void run(){
tab.clear(); tab.resize(7);
s.clear();
int cur = 0;
while (++cnt, true) {
int x = hand.front(); hand.pop_front();
tab[cur].push_back(x);
while (tab[cur].size() >= 3 && match(tab[cur]));
if (hand.size() == 52) { printf("Win : %d\n", cnt); return; }
if (hand.size() == 0) { printf("Loss: %d\n", cnt); return; }
vector<deque<int>> t(tab); t.push_back(hand);
if (s.count(t)) { printf("Draw: %d\n", cnt); return; }
else s.insert(t);
do {
cur = (cur + 1) % 7;
} while (tab[cur].size() == 0 && cnt >= 8);
}
}
int main()
{
ios::sync_with_stdio(false);
while (input())
run();
return 0;
}