-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhacamole.cc
245 lines (187 loc) · 6.06 KB
/
whacamole.cc
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// William Sjöblom
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <iostream>
#include <numeric>
#include <cmath>
#include <string>
#include <iomanip>
struct Mole {
int x, y;
int t;
};
const int MAX_TIME = 10;
const int MAX_WIDTH = 20;
const int MAX_DISTANCE = 5;
struct Move {
int dx, dy;
float distance;
Move(int dx, int dy) : dx(dx),
dy(dy),
distance(sqrtf(dx*dx + dy*dy)) { }
void dump() {
std::cout << "|(" << dx << ", " << dy << ")| = " << distance;
}
};
/**
* has_mole[t][x][y] is true if and only if (x, y) has a mole at time 't'.
*/
bool has_mole[MAX_TIME][MAX_WIDTH + 2*MAX_DISTANCE][MAX_WIDTH + 2*MAX_DISTANCE];
/**
* hit_moles[t][x][y] is the maximum number of hit moles if the player
* moved the hammer to (x, y) during the last time unit.
*/
int hit_moles[MAX_TIME + 1][MAX_WIDTH + 2*MAX_DISTANCE][MAX_WIDTH + 2*MAX_DISTANCE];
void dump_hit_matrix(int t, int width, int max_distance) {
const int start = MAX_DISTANCE - max_distance;
const int stop = start + width + 2*max_distance;
printf("t = %d:\n", t);
for (int x = start; x < stop; x++) {
for (int y = start; y < stop; y++) {
printf("%01d ", hit_moles[t][x][y]);
}
printf("\n");
}
}
void dump_matrices(int t, int width, int max_distance) {
const int start = MAX_DISTANCE - max_distance;
const int stop = start + width + 2*max_distance;
printf("t = %d:\n", t);
for (int y = start; y < stop; y++) {
for (int x = start; x < stop; x++) {
printf("%01d ", hit_moles[t][x][y]);
}
printf(" | ");
for (int x = start; x < stop; x++) {
printf("%01d ", has_mole[t][x][y]);
}
printf("\n");
}
}
/**
* True if there is a mole at position (x, y) at time 't'.
*/
inline bool mole(int t, int x, int y) {
return has_mole[t][MAX_DISTANCE + x][MAX_DISTANCE + y];
}
/**
* Assign a mole at position (x, y) at time 't'.
*/
inline void set_mole(int t, int x, int y) {
has_mole[t][MAX_DISTANCE + x][MAX_DISTANCE + y] = true;
}
/**
* Maximum numbers of moles hit if the starting position at time 't'
* is (x, y).
*/
inline int mole_count(int t, int x, int y) {
return hit_moles[t][MAX_DISTANCE + x][MAX_DISTANCE + y];
}
/**
* Set the mole count at (x, y) at time 't' to 'v' if the new value
* is greater than the previous.
*/
inline void update_mole_count(int t, int x, int y, int v) {
int& count = hit_moles[t][MAX_DISTANCE + x][MAX_DISTANCE + y];
count = std::max(count, v);
}
/**
* Populate hit_moles where (x, y) is the position at the start of
* timestep 't'.
*/
inline void solve0(int t, int x, int y, int max_distance, std::vector<Move>& moves) {
// Zero move.
if (mole(t, x, y)) {
update_mole_count(t + 1, x, y, mole_count(t, x, y) + 1);
} else {
update_mole_count(t + 1, x, y, mole_count(t, x, y));
}
bool v = x == 0 && y == 0 && false;
for (const Move& m : moves) {
int new_x = x + m.dx;
int new_y = y + m.dy;
int hits_in_dir = mole_count(t, x, y);
if (mole(t, x, y))
hits_in_dir++;
float distance = m.distance;
while (distance <= max_distance) {
if (mole(t, new_x, new_y))
hits_in_dir++;
update_mole_count(t + 1, new_x, new_y, hits_in_dir);
if (v) printf("%d ", hits_in_dir);
new_x += m.dx;
new_y += m.dy;
distance += m.distance;
}
}
}
int solve(int width, int max_distance,
std::vector<Mole>& moles, std::vector<Move>& moves) {
// Don't forget to zero out matrices between test cases!
// Initialize 'has_mole' matrix.
for (auto& mole : moles) {
set_mole(mole.t, mole.x, mole.y);
}
// Does not need to be recalculated at each test case
// but should not cause a significant performance hit.
const int start = -max_distance + 1;
const int stop = width + max_distance;
for (int t = 0; t < MAX_TIME; t++) {
for (int x = start; x < stop; x++) {
for (int y = start; y < stop; y++) {
solve0(t, x, y, max_distance, moves);
}
}
}
int result = 0;
for (int x = 0; x < MAX_WIDTH + 2*MAX_DISTANCE; x++) {
for (int y = 0; y < MAX_WIDTH + 2*MAX_DISTANCE; y++) {
result = std::max(hit_moles[10][x][y], result);
}
}
return result;
}
int main() {
std::vector<Move> moves;
int sq_max_distance = MAX_DISTANCE*MAX_DISTANCE;
for (int x = 0; x*x <= sq_max_distance; x++) {
for (int y = 0; y*y + x*x <= sq_max_distance; y++) {
// Skip all overlapping (and zero moves since __gcd(0, 0) = 0 != 1).
if (std::__gcd(x, y) != 1) continue;
moves.push_back({x, y});
if (x > 0) moves.push_back({-x, y});
if (y > 0) moves.push_back({x, -y});
if (x > 0 && y > 0) moves.push_back({-x, -y});
}
}
while (true) {
int width, max_distance, mole_count;
scanf("%d %d %d", &width, &max_distance, &mole_count);
if (width == 0 &&
max_distance == 0 &&
mole_count == 0) break;
std::vector<Mole> moles;
moles.reserve(mole_count);
while (mole_count--) {
Mole m;
scanf("%d %d %d", &m.x, &m.y, &m.t);
m.t--; // Make time a zero index.
moles.push_back(m);
}
int result = solve(width, max_distance,
moles, moves);
printf("%d\n", result);
for (int t = 0; t < MAX_TIME + 1; t++) {
for (int x = 0; x < MAX_WIDTH + 2*MAX_DISTANCE; x++) {
for (int y = 0; y < MAX_WIDTH + 2*MAX_DISTANCE; y++) {
if (t < 10)
has_mole[t][x][y] = false;
hit_moles[t][x][y] = 0;
}
}
}
}
return 0;
}