对于每个手牌,直接用双向队列去维护,然后用vactor去维护所有小牌堆,对于vactor存储的全部状态,用set去重来保证不达到重复状态。其他的步骤直接模拟完成即可。
#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(1) {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); } 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); int cur=0; s.clear(); 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() { while (input()) run(); return 0; }