forked from mcarton/Hashcode-final-round
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_prog.py
executable file
·145 lines (109 loc) · 4.92 KB
/
dynamic_prog.py
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
#!/usr/bin/env python3
import sys
class Point:
def __init__(self, i, j):
self.i = i
self.j = j
def valid(self, problem):
return self.i >= 0 and self.i < problem.rows and self.j >= 0 and self.j < problem.cols
def __repr__(self):
return 'Point(%d, %d)' % (self.i, self.j)
class Problem:
def __init__(self, rows, cols, altitudes, targets, radius, num_ballons, turns, starting_cell, mov_grids):
self.rows = rows
self.cols = cols
self.altitudes = altitudes
self.targets = targets
self.radius = radius
self.num_ballons = num_ballons
self.turns = turns
self.starting_cell = starting_cell
self.mov_grids = mov_grids
def parse_problem(f):
rows, cols, altitudes = map(int, f.readline().strip().split())
num_targets, radius, num_ballons, turns = map(int, f.readline().strip().split())
starting_cell_i, starting_cell_j = map(int, f.readline().strip().split())
targets = []
for _ in range(num_targets):
i, j = map(int, f.readline().strip().split())
targets.append(Point(i, j))
mov_grids = []
mov_grids.append([]) # altitude 0
for _ in range(altitudes):
grid = []
for _ in range(rows):
in_ = list(map(int, f.readline().strip().split()))
wind = []
while in_:
wind.append(Point(in_[0], in_[1]))
in_ = in_[2:]
grid.append(wind)
mov_grids.append(grid)
return Problem(rows, cols, altitudes, targets, radius, num_ballons, turns,
Point(starting_cell_i, starting_cell_j), mov_grids)
def solution_balloon(problem, starting_cell):
possibilities = {}
possibilities[starting_cell.i, starting_cell.j, 0] = 0
#parent = [{} for _ in range(problem.turns)]
# mask for covered cells
mask_cells = []
for i in range(-problem.radius, problem.radius + 1):
for j in range(-problem.radius, problem.radius + 1):
if i**2 + j**2 <= problem.radius**2:
mask_cells.append((i, j))
# map of targets
target_map = [[False for _ in range(problem.cols)] for _ in range(problem.rows)]
for t in problem.targets:
target_map[t.i][t.j] = True
# score map
score_map = [[0 for _ in range(problem.cols)] for _ in range(problem.rows)]
for i in range(problem.rows):
for j in range(problem.cols):
score_map[i][j] = 0
for di, dj in mask_cells:
if i + di >= 0 and i + di < problem.rows and target_map[i + di][(j + dj) % problem.cols]:
score_map[i][j] += 1
for turn_id in range(problem.turns):
new_possibilities = {}
for (i, j, alt), score in possibilities.items():
# no move #########
nalt = alt
ni = i + problem.mov_grids[nalt][i][j].i if nalt > 0 else i
nj = (j + problem.mov_grids[nalt][i][j].j) % problem.cols if nalt > 0 else j
if ni >= 0 and ni < problem.rows:
nscore = score + score_map[ni][nj]
if (ni, nj, nalt) not in new_possibilities or nscore > new_possibilities[ni, nj, nalt]:
new_possibilities[ni, nj, nalt] = nscore
#parent[turn_id][ni, nj, nalt] = (i, j, alt), 0
# moving up #######
if alt < problem.altitudes:
nalt = alt + 1
ni = i + problem.mov_grids[nalt][i][j].i
nj = (j + problem.mov_grids[nalt][i][j].j) % problem.cols
if ni >= 0 and ni < problem.rows:
nscore = score + score_map[ni][nj]
if (ni, nj, nalt) not in new_possibilities or nscore > new_possibilities[ni, nj, nalt]:
new_possibilities[ni, nj, nalt] = nscore
#parent[turn_id][ni, nj, nalt] = (i, j, alt), 1
# moving down #######
if alt >= 2:
nalt = alt - 1
ni = i + problem.mov_grids[nalt][i][j].i
nj = (j + problem.mov_grids[nalt][i][j].j) % problem.cols
if ni >= 0 and ni < problem.rows:
nscore = score + score_map[ni][nj]
if (ni, nj, nalt) not in new_possibilities or nscore > new_possibilities[ni, nj, nalt]:
new_possibilities[ni, nj, nalt] = nscore
#parent[turn_id][ni, nj, nalt] = (i, j, alt), -1
possibilities = new_possibilities
print('turn %d, %d possibilities' % (turn_id, len(possibilities)))
print(possibilities)
(i, j, alt), best_score = max(possibilities.items(), key=lambda v: v[1])
print('best_score %d' % best_score)
if __name__ == '__main__':
if len(sys.argv) < 2:
print('usage: %s FILE' % sys.argv[0], file=sys.stderr)
exit(1)
with open(sys.argv[1], 'r') as f:
problem = parse_problem(f)
solution_balloon(problem, problem.starting_cell)