-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-246.cpp
113 lines (107 loc) · 2.23 KB
/
UVA-246.cpp
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
#include<stdio.h>
#include<deque>
#include<vector>
#include<set>
using namespace std;
vector<deque<int>> vec;
set<vector<deque<int>>> se;
int gameNum;
int stepPlus(int step) {
step = step + 1;
if(step == 8) step = 1;
return step;
}
void handlePile(int step) {
int num, k1, k2, k3;
while(1) {
num = vec[step].size();
if(num < 3) return;
if((vec[step][0] + vec[step][1] + vec[step][num - 1]) % 10 == 0) {
k1 = vec[step].front();
vec[step].pop_front();
vec[0].push_back(k1);
k1 = vec[step].front();
vec[step].pop_front();
vec[0].push_back(k1);
k1 = vec[step].back();
vec[step].pop_back();
vec[0].push_back(k1);
continue;
}
if((vec[step][0] + vec[step][num - 1] + vec[step][num - 2]) % 10 == 0) {
k1 = vec[step].front();
vec[step].pop_front();
vec[0].push_back(k1);
k2 = vec[step].back();
vec[step].pop_back();
k3 = vec[step].back();
vec[step].pop_back();
vec[0].push_back(k3);
vec[0].push_back(k2);
continue;
}
if((vec[step][num - 3] + vec[step][num - 1] + vec[step][num - 2]) % 10 == 0) {
k1 = vec[step].back();
vec[step].pop_back();
k2 = vec[step].back();
vec[step].pop_back();
k3 = vec[step].back();
vec[step].pop_back();
vec[0].push_back(k3);
vec[0].push_back(k2);
vec[0].push_back(k1);
continue;
}
return;
}
}
int handleGame() {
int k;
int step = 1;
while(!vec[0].empty()) {
++gameNum;
k = vec[0].front();
vec[0].pop_front();
while(vec[step].empty()) step = stepPlus(step);
vec[step].push_back(k);
handlePile(step);
// draw
if(se.count(vec)) return 0;
se.insert(vec);
step = stepPlus(step);
if(vec[0].size() == 52) {
// win
return 1;
}
}
// lose
return -1;
}
int main() {
int k, i, j;
while(scanf("%d", &k) == 1 && k != 0) {
gameNum = 7;
vec.clear();
se.clear();
vec.push_back(deque<int>());
vec.push_back(deque<int>());
vec[1].push_back(k);
for(i = 2; i < 8; ++i) {
scanf("%d", &k);
vec.push_back(deque<int>());
vec[i].push_back(k);
}
for(i = 8; i <= 52; ++i) {
scanf("%d", &k);
vec[0].push_back(k);
}
k = handleGame();
switch(k) {
case -1: printf("Loss"); break;
case 0: printf("Draw"); break;
case 1: printf("Win "); break;
}
printf(": %d\n", gameNum);
}
return 0;
}