-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhypersonic.rs
389 lines (367 loc) · 12.4 KB
/
hypersonic.rs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//extern crate rand;
use std::io;
//use rand::Rng;
macro_rules! print_err {
($($arg:tt)*) => (
{
use std::io::Write;
writeln!(&mut ::std::io::stderr(), $($arg)*).ok();
}
)
}
macro_rules! parse_input {
($x:expr, $t:ident) => ($x.trim().parse::<$t>().unwrap())
}
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fn main() {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let inputs = input_line.split(" ").collect::<Vec<_>>();
//let width = parse_input!(inputs[0], i32);
let height = parse_input!(inputs[1], i32);
let my_id = parse_input!(inputs[2], i32);
let mut pos_x = 0;
let mut pos_y = 0;
if my_id == 1 {
pos_x = 12;
pos_y = 10;
}
let mut range = 3;
// game loop
loop {
// TODO: Learn how to create [vec![]; 10]
let mut danger = [[0; 11]; 13];
let mut free = [['.'; 11]; 13];
let mut boxes_left = false;
for i in 0..height as usize {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let row = input_line.trim_right().to_string();
for (j, c) in row.chars().enumerate() {
if is_box(c) {
boxes_left = true;
}
free[j][i] = c;
}
}
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let entities = parse_input!(input_line, i32);
let mut bombs = vec![];
//TODO: Bomb if will trap an enemy.
let mut enemies = vec![];
for _ in 0..entities as usize {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let inputs = input_line.split(" ").collect::<Vec<_>>();
let entity_type = parse_input!(inputs[0], i32);
let owner = parse_input!(inputs[1], i32);
let x = parse_input!(inputs[2], usize);
let y = parse_input!(inputs[3], usize);
let param_1 = parse_input!(inputs[4], i32);
let param_2 = parse_input!(inputs[5], i32);
if entity_type == 0 {
if owner == my_id {
pos_x = x;
pos_y = y;
range = param_2;
} else {
enemies.push((x, y, range));
}
} else if entity_type == 1 {
// print_err!("Bomb {} {} timer {}", x, y, param_1);
free[x][y] = 'B';
//TODO: Use insertion sort as seems to be sorted now by timer, but we shouldn't count on that ...
bombs.push((x, y, param_1, param_2));
} else if entity_type == 2 {
// print_err!("Power up on {} {} type {}", x, y, param_1);
free[x][y] = 'P';
}
/*
if entity_type == 2 {
if dst(x, pos_x, y, pos_y) < dist {
dist = dst(x, pos_x, y, pos_y);
target_x = x;
target_y = y;
}
}
if entity_type == 2 && target_x == x && target_y == y {
found = true;
}
*/
}
for (x, y, timer, range) in bombs {
add_bomb(x, y, &mut danger, free, timer, range);
}
// TODO: It is important to pick up type 2 bombs when we can carry < 3
/*
if target_x < 0 {
target_x = rand::thread_rng().gen_range(0, 13);
target_y = rand::thread_rng().gen_range(0, 11);
let (safe_x, safe_y) = safe(target_x, target_y, danger, free);
target_x = safe_x;
target_y = safe_y;
}
*/
//Check if I am in danger.
if danger[pos_x][pos_y] != 0 {
// Find the closest safe place.
let mut safe_cell = safe_target(pos_x, pos_y, danger, free, boxes_left);
if boxes_left && safe_cell == (pos_x, pos_y) {
safe_cell = safe_target(pos_x, pos_y, danger, free, false);
}
if is_box_in_range(pos_x, pos_y, danger, free, range) {
add_bomb(pos_x, pos_y, &mut danger, free, 8, range);
let bombed_safe_cell = safe_target(pos_x, pos_y, danger, free, false);
if bombed_safe_cell != (pos_x, pos_y) {
println!("BOMB {} {}", bombed_safe_cell.0, bombed_safe_cell.1);
} else {
println!("MOVE {} {}", safe_cell.0, safe_cell.1);
}
} else {
println!("MOVE {} {}", safe_cell.0, safe_cell.1);
}
} else {
if boxes_left {
let mut safe_cell = safe_target(pos_x, pos_y, danger, free, true);
if safe_cell == (pos_x, pos_y) {
safe_cell = safe_target(pos_x, pos_y, danger, free, false);
}
if is_box_in_range(pos_x, pos_y, danger, free, range) {
add_bomb(pos_x, pos_y, &mut danger, free, 8, range);
for (enemy_x, enemy_y, enemy_range) in enemies {
add_bomb(enemy_x, enemy_y, &mut danger, free, 8, enemy_range)
}
let mut bombed_safe_cell = safe_target(pos_x, pos_y, danger, free, true);
if bombed_safe_cell == (pos_x, pos_y) {
bombed_safe_cell = safe_target(pos_x, pos_y, danger, free, false);
}
if bombed_safe_cell != (pos_x, pos_y) {
println!("BOMB {} {}", bombed_safe_cell.0, bombed_safe_cell.1);
} else {
println!("MOVE {} {}", safe_cell.0, safe_cell.1);
}
} else {
println!("MOVE {} {}", safe_cell.0, safe_cell.1);
}
} else {
println!("MOVE {} {} MOJITO TIME", pos_x, pos_y);
}
}
}
}
// Manhattan distance as there is no diagonal moves.
/*
fn dst(x1:i32, y1:i32, x2:i32, y2:i32) -> i32 {
return (x2-x1).abs() + (y2-y1).abs();
}
*/
// TODO: return next box without bomb.
fn safe_target(x: usize, y: usize, danger:[[i32; 11]; 13], free:[[char; 11]; 13], find_box: bool) -> (usize, usize) {
let c0 = (x, y, 2, (x, y));
let mut q = vec![c0];
let mut visited = [[false; 11]; 13];
while let Some(cell) = q.pop() {
if visited[cell.0][cell.1] {
continue;
}
if danger[cell.0][cell.1] == 0 && is_walkable(free[cell.0][cell.1]) && cell != c0 {
// print_err!("No danger found on {} {} steps {}", cell.0, cell.1, cell.2 - 2);
if !find_box || is_adj_box(cell.0, cell.1, danger, free) || free[cell.0][cell.1] == 'P' || cell.2 > 20 {
// print_err!("Returning...");
return cell.3;
}
// print_err!("But we are looking for something to blow up...");
}
let mut first_step = cell.3;
// LEFT
if cell.0 != 0 && is_step_safe(cell.0 - 1, cell.1, danger, free, cell.2) {
if cell.2 == 2 {
first_step = (cell.0 - 1, cell.1);
}
q.insert(0, (cell.0 - 1, cell.1, cell.2 + 1, first_step));
}
// DOWN
if cell.1 != 10 && is_step_safe(cell.0, cell.1 + 1, danger, free, cell.2) {
if cell.2 == 2 {
first_step = (cell.0, cell.1 + 1);
}
q.insert(0, (cell.0, cell.1 + 1, cell.2 + 1, first_step));
}
// RIGHT
if cell.0 != 12 && is_step_safe(cell.0 + 1, cell.1, danger, free, cell.2) {
if cell.2 == 2 {
first_step = (cell.0 + 1, cell.1);
}
q.insert(0, (cell.0 + 1, cell.1, cell.2 + 1, first_step));
}
// UP
if cell.1 != 0 && is_step_safe(cell.0, cell.1 - 1, danger, free, cell.2) {
if cell.2 == 2 {
first_step = (cell.0, cell.1 - 1);
}
q.insert(0, (cell.0, cell.1 - 1, cell.2 + 1, first_step));
}
visited[cell.0][cell.1] = true;
}
// print_err!("No safe cell found");
return (x, y);
}
// TODO: Maybe we should take into account to stop at power ups...
fn add_bomb(x: usize, y: usize, danger: &mut [[i32; 11]; 13], free: [[char; 11]; 13], mut timer: i32, range: i32) {
if danger[x][y] != 0 {
timer = danger[x][y];
}
let mut min32 = x as i32 - (range - 1);
let mut min = 0;
if min32 > 0 {
min = min32 as usize;
}
let mut max = x + range as usize;
if max > 13 {
max = 13;
}
for j in (min..x).rev() {
danger[j][y] = set_timer(j, y, danger, timer);
if !is_walkable(free[j][y]) {
break;
}
}
for j in x..max {
danger[j][y] = set_timer(j, y, danger, timer);
if j != x && !is_walkable(free[j][y]) {
break;
}
}
min32 = y as i32 - (range - 1);
min = 0;
if min32 > 0 {
min = min32 as usize;
}
max = y + range as usize;
if max > 11 {
max = 11;
}
for j in (min..y).rev() {
danger[x][j] = set_timer(x, j, danger, timer);
if !is_walkable(free[x][j]) {
break;
}
}
for j in y..max {
danger[x][j] = set_timer(x, j, danger, timer);
if j != y && !is_walkable(free[x][j]) {
break;
}
}
}
fn set_timer(x: usize, y: usize, danger: &mut [[i32; 11]; 13], timer: i32) -> i32 {
if danger[x][y] == 0 {
return timer;
}
return danger[x][y] * 10 + timer;
}
fn is_adj_box(x: usize, y: usize, danger: [[i32; 11]; 13], free: [[char; 11]; 13]) -> bool {
if x != 0 && danger[x-1][y] == 0 && is_box(free[x-1][y]) {
return true;
} else if x != 12 && danger[x+1][y] == 0 && is_box(free[x+1][y]) {
return true;
} else if y != 0 && danger[x][y-1] == 0 && is_box(free[x][y-1]) {
return true;
} else if y != 10 && danger[x][y+1] == 0 && is_box(free[x][y+1]) {
return true;
}
return false;
}
fn is_box_in_range(x: usize, y: usize, danger: [[i32; 11]; 13], free: [[char; 11]; 13], range: i32) -> bool {
let mut min32 = x as i32 - (range - 1);
let mut min = 0;
if min32 > 0 {
min = min32 as usize;
}
let mut max = x + range as usize;
if max > 13 {
max = 13;
}
for j in (min..x).rev() {
if danger[j][y] == 0 && is_box(free[j][y]) {
// print_err!("box found {} {} -> {} {}", x, y, j, y);
return true;
}
if !is_free(free[j][y]) {
break;
}
}
for j in x..max {
if danger[j][y] == 0 && is_box(free[j][y]) {
// print_err!("box found {} {} -> {} {}", x, y, j, y);
return true;
}
if j != x && !is_free(free[j][y]) {
break;
}
}
min32 = y as i32 - (range - 1);
min = 0;
if min32 > 0 {
min = min32 as usize;
}
max = y + range as usize;
if max > 11 {
max = 11;
}
for j in (min..y).rev() {
if danger[x][j] == 0 && is_box(free[x][j]) {
// print_err!("box found {} {} -> {} {}", x, y, x, j);
return true;
}
if !is_free(free[x][j]) {
break;
}
}
for j in y..max {
if danger[x][j] == 0 && is_box(free[x][j]) {
// print_err!("box found {} {} -> {} {}", x, y, x, j);
return true;
}
if j != y && !is_free(free[x][j]) {
break;
}
}
return false;
}
fn is_box(c: char) -> bool {
match c {
'0' => true,
'1' => true,
'2' => true,
_ => false,
}
}
fn is_free(c: char) -> bool {
return c == '.';
}
fn is_walkable(c: char) -> bool {
match c {
'.' => true,
'P' => true,
_ => false,
}
}
fn is_step_safe(x: usize, y: usize, danger: [[i32; 11]; 13], free: [[char; 11]; 13], steps: i32) -> bool {
// print_err!("Is step safe {},{} danger: {} steps: {}", x, y, danger[x][y], steps);
if !is_walkable(free[x][y]) {
return false;
}
let mut d = danger[x][y];
while d != 0 {
if d % 10 == steps {
return false;
}
d = d/10;
}
return true;
}