UVa246 - 10-20-30
题设
A simple solitaire card game called 10-20-30 uses a standard deck of 52 playing cards in which suit is irrelevant. The value of a face card (king, queen, jack) is 10. The value of an ace is one. The value of each of the other cards is the face value of the card (2, 3, 4, etc.). Cards are dealt from the top of the deck. You begin by dealing out seven cards, left to right forming seven piles. After playing a card on the rightmost pile, the next pile upon which you play a card is the leftmost pile.
For each card placed on a pile, check that pile to see if one of the following three card combinations totals 10, 20, or 30.
- the first two and last one,
- the first one and the last two, or
- the last three cards.
If so, pick up the three cards and place them on the bottom of the deck. For this problem, always check the pile in the order just described. Collect the cards in the order they appear on the pile and put them at the bottom of the deck. Picking up three cards may expose three more cards that can be picked up. If so, pick them up. Continue until no more sets of three can be picked up from the pile.
For example, suppose a pile contains 5 9 7 3 where the 5 is at the first card of the pile, and then a 6 is played. The first two cards plus the last card (5 + 9 + 6) sum to 20. The new contents of the pile after picking up those three cards becomes 7 3. Also, the bottommost card in the deck is now the 6, the card above it is the 9, and the one above the 9 is the 5.
If a queen were played instead of the six, 5 + 9 + 10 = 24, and 5 + 3 + 10 = 18, but 7 + 3 + 10 = 20, so the last three cards would be picked up, leaving the pile as 5 9.
If a pile contains only three cards when the three sum to 10, 20, or 30, then the pile ”disappears” when the cards are picked up. That is, subsequent play skips over the position that the now-empty pile occupied. You win if all the piles disappear. You lose if you are unable to deal a card. It is also possible to have a draw if neither of the previous two conditions ever occurs.
Write a program that will play games of 10-20-30 given initial card decks as input.
Input
Each input set consists of a sequence of 52 integers separated by spaces and/or ends of line. The integers represent card values of the initial deck for that game. The first integer is the top card of the deck. Input is terminated by a single zero (0) following the last deck.
Output
For each input set, print whether the result of the game is a win, loss, or a draw, and print the number of times a card is dealt before the game results can be determined. (A draw occurs as soon as the state of the game is repeated.) Use the format shown in the “Sample Output” section.
Sample Input
1
2
3
4
5
6
7
2 6 5 10 10 4 10 10 10 4 5 10 4 5 10 9 7 6 1 7 6 9 5 3 10 10 4 10 9 2 1
10 1 10 10 10 3 10 9 8 10 8 7 1 2 8 6 7 3 3 8 2
4 3 2 10 8 10 6 8 9 5 8 10 5 3 5 4 6 9 9 1 7 6 3 5 10 10 8 10 9 10 10 7
2 6 10 10 4 10 1 3 10 1 1 10 2 2 10 4 10 7 7 10
10 5 4 3 5 7 10 8 2 3 9 10 8 4 5 1 7 6 7 2 6 9 10 2 3 10 3 4 4 9 10 1 1
10 5 10 10 1 8 10 7 8 10 6 10 10 10 9 6 2 10 10
0
Sample Output
1
2
3
Win : 66
Loss: 82
Draw: 73
题解
这道题也是模拟,需要维护的数据有:手上的牌、7个牌堆中的牌、游戏出现过的状态的集合。分别用queue、deque和set来记录,其中状态的记录用了转化为字符串的方式,详见convert函数。这里学习了一个新知识:queue容器没有迭代器,但如果需要对其中元素进行迭代,可以取出内部容器,方法见convert函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <bitset>
#include <deque>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
int t = -1, cnt;
queue<int> q;
deque<int> pile[7];
bitset<7> st;
set<string> s;
string convert() {
string str;
for (int i = 0; i < 7; i++) {
for (auto& it : pile[i]) {
str.push_back(it % 10 + '0');
str.push_back('#');
}
str.push_back('#');
}
str.push_back('#');
deque<int>& cq = *(reinterpret_cast<deque<int>*>(&q));
for (auto& it : cq) {
str.push_back(it % 10 + '0');
str.push_back('#');
}
return str;
}
void nexti() {
do {
t = (t + 1) % 7;
} while (!st[t]);
}
bool check(int a, int b, int c) { return !((a + b + c) % 10); }
void init() {
st.set();
t = -1;
q = queue<int>();
cnt = 14;
for (int i = 0; i < 7; i++) pile[i].clear();
s.clear();
}
void check_comb(deque<int>& p) {
while (p.size() >= 3) {
int n = p.size();
if ((p[0] + p[1] + p[n - 1]) % 10 == 0) {
q.push(p[0]), q.push(p[1]), q.push(p[n - 1]);
p.pop_front();
p.pop_front();
p.pop_back();
} else if ((p[0] + p[n - 2] + p[n - 1]) % 10 == 0) {
q.push(p[0]), q.push(p[n - 2]), q.push(p[n - 1]);
p.pop_front();
p.pop_back();
p.pop_back();
} else if ((p[n - 3] + p[n - 2] + p[n - 1]) % 10 == 0) {
q.push(p[n - 3]), q.push(p[n - 2]), q.push(p[n - 1]);
p.pop_back();
p.pop_back();
p.pop_back();
} else {
break;
}
}
}
int check_state() {
if (st.none()) return 1;
string state = convert();
if (s.count(state)) return 3;
s.insert(state);
return 0;
}
int sim() {
nexti();
if (q.empty()) return 2;
pile[t].push_back(q.front());
q.pop();
check_comb(pile[t]);
if (pile[t].size() == 0) st.reset(t);
cnt++;
int a = check_state();
return a;
}
int main() {
int x;
while (scanf("%d", &x) && x) {
init();
q.push(x);
for (int i = 0; i < 51; i++) {
scanf("%d", &x);
q.push(x);
}
for (int i = 0; i < 7; i++) {
pile[i].push_back(q.front());
q.pop();
}
for (int i = 0; i < 7; i++) {
pile[i].push_back(q.front());
q.pop();
}
while (true) {
int res = sim();
if (res == 0)
continue;
else {
if (res == 1)
printf("Win : %d\n", cnt);
else if (res == 2)
printf("Loss: %d\n", cnt);
else
printf("Draw: %d\n", cnt);
break;
}
}
}
return 0;
}