-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
335 lines (294 loc) · 9.99 KB
/
main.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
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
import math
import queue
def pacman1():
for i in range(1, 6):
f = open('level1_' + str(i) + '.in', 'r')
n = f.readline()
counter = 0
for line in f:
for char in line:
if char == 'C':
counter += 1
out = open('level1_' + str(i) + '.out', 'w')
out.write(str(counter))
out.close()
print(counter)
def pacman2():
for i in range(1, 6):
f = open('data/level2_' + str(i) + '.in', 'r')
n = int(f.readline())
mapa = []
counter = 0
for _ in range(n):
mapa.append(f.readline())
y, x = map(int, f.readline().split())
com_count = int(f.readline())
coms = f.readline()
print(coms)
x -= 1
y -= 1
coins = []
for let in coms:
if let == 'U':
y -= 1
if let == 'D':
y += 1
if let == 'R':
x += 1
if let == 'L':
x -= 1
if mapa[y][x] == 'C':
coins.append((y, x))
set_coins = set(coins)
ans = len(set_coins)
out = open('data/level2_' + str(i) + '.out', 'w')
out.write(str(ans))
out.close()
print(ans)
def pacman3():
for m in range(1, 8):
f = open('data/level3_' + str(m) + '.in', 'r')
#f = open('data/level3_example.in', 'r')
n = int(f.readline())
mapa = []
coins = []
ghost_pos = []
ghost_coms = []
alive = True
for _ in range(n):
mapa.append(f.readline())
y, x = map(int, f.readline().split())
x -= 1
y -= 1
com_len = int(f.readline())
coms = f.readline()
ghosts = int(f.readline())
ghost_pos.append([y, x])
ghost_coms.append(coms)
for _ in range(ghosts):
g_y, g_x = map(int, f.readline().split())
g_y -= 1
g_x -= 1
ghost_pos.append([g_y, g_x])
_ = int(f.readline())
ghost_coms.append(f.readline())
for i in range(com_len):
for j in range(ghosts + 1):
if ghost_coms[j][i] == 'U':
ghost_pos[j][0] -= 1
if ghost_coms[j][i] == 'D':
ghost_pos[j][0] += 1
if ghost_coms[j][i] == 'R':
ghost_pos[j][1] += 1
if ghost_coms[j][i] == 'L':
ghost_pos[j][1] -= 1
for j in range(1, ghosts + 1):
if ghost_pos[0] == ghost_pos[j]:
alive = False
if not alive:
break
if mapa[ghost_pos[0][0]][ghost_pos[0][1]] == 'C':
coins.append((ghost_pos[0][0], ghost_pos[0][1]))
set_coins = set(coins)
ans = len(set_coins)
out = open('data/level3_' + str(m) + '.out', 'w')
out.write(str(ans))
out.write(' YES' if alive else ' NO')
out.close()
print(ans)
print(alive)
def shortest_dist(y, x, mapa):
visited = [[False for j in range(len(mapa))] for i in range(len(mapa))]
stack = [[y, x]]
p = []
while len(stack):
s = stack[-1]
stack.pop()
if not visited[s[0]][s[1]]:
print(s, end=' ')
p.append(s)
visited[s[0]][s[1]] = True
adj = []
if mapa[s[0]][s[1]+1] != 'W' and mapa[s[0]][s[1]+1] != 'G':
adj.append([s[0], s[1]+1])
if mapa[s[0]][s[1] - 1] != 'W' and mapa[s[0]][s[1] - 1] != 'G':
adj.append([s[0], s[1]-1])
if mapa[s[0] + 1][s[1]] != 'W' and mapa[s[0] + 1][s[1]] != 'G':
adj.append([s[0]+1, s[1]])
if mapa[s[0] - 1][s[1]] != 'W' and mapa[s[0] - 1][s[1]] != 'G':
adj.append([s[0]-1, s[1]])
for node in adj:
if not visited[node[0]][node[1]]:
stack.append(node)
return p
def shortest_dist_two_points(start, end, mapa):
queue = [[start]]
while queue:
path = queue.pop(0)
u = path[-1]
adj = []
if mapa[u[0]][u[1] + 1] != 'W' and mapa[u[0]][u[1] + 1] != 'G':
adj.append([u[0], u[1] + 1])
if mapa[u[0]][u[1] - 1] != 'W' and mapa[u[0]][u[1] - 1] != 'G':
adj.append([u[0], u[1] - 1])
if mapa[u[0] + 1][u[1]] != 'W' and mapa[u[0] + 1][u[1]] != 'G':
adj.append([u[0] + 1, u[1]])
if mapa[u[0] - 1][u[1]] != 'W' and mapa[u[0] - 1][u[1]] != 'G':
adj.append([u[0] - 1, u[1]])
for neighbour in adj:
new_path = list(path)
new_path.append(neighbour)
queue.append(new_path)
if neighbour == end:
return new_path
return
def decode(p, mapa):
base = p[0]
new_path = []
for tup in p[1:]:
x_dif = base[1] - tup[1]
y_dif = base[0] - tup[0]
if x_dif == 1 and y_dif == 0:
new_path.append('L')
elif x_dif == -1 and y_dif == 0:
new_path.append('R')
elif x_dif == 0 and y_dif == 1:
new_path.append('U')
elif x_dif == 0 and y_dif == -1:
new_path.append('D')
else:
small_path = shortest_dist_two_points(base, tup, mapa)
for coord in small_path[1:]:
x_dif = base[1] - coord[1]
y_dif = base[0] - coord[0]
if x_dif == 1 and y_dif == 0:
new_path.append('L')
elif x_dif == -1 and y_dif == 0:
new_path.append('R')
elif x_dif == 0 and y_dif == 1:
new_path.append('U')
elif x_dif == 0 and y_dif == -1:
new_path.append('D')
base = coord[:]
base = tup[:]
# small_path = shortest_dist_two_points(p[-1], p[0], mapa)
# for coord in small_path[1:]:
# x_dif = base[1] - coord[1]
# y_dif = base[0] - coord[0]
# if x_dif == 1 and y_dif == 0:
# new_path.append('L')
# elif x_dif == -1 and y_dif == 0:
# new_path.append('R')
# elif x_dif == 0 and y_dif == 1:
# new_path.append('U')
# elif x_dif == 0 and y_dif == -1:
# new_path.append('D')
# base = coord[:]
return new_path
def pacman4():
for i in range(1, 6):
f = open('data/level4_' + str(i) + '.in', 'r')
#f = open('data/level4_example.in', 'r')
n = int(f.readline())
mapa = []
counter = 0
for _ in range(n):
mapa.append(f.readline())
y, x = map(int, f.readline().split())
com_count = int(f.readline())
coms = f.readline()
print(coms)
x -= 1
y -= 1
aws_tab = decode(shortest_dist(y,x,mapa), mapa)
ans = ''
for el in aws_tab:
ans += el
print(ans)
out = open('data/level4_' + str(i) + '.out', 'w')
out.write(str(ans))
out.close()
print(ans)
def ghost_positions(ghosts, ghost_pos, ghost_coms):
ans = []
for i in range(ghosts):
pos = [ghost_pos[i]]
for j in range(len(ghost_coms[i])):
if ghost_coms[i][j] == 'U':
pos.append([pos[-1][0] - 1, pos[-1][1]])
if ghost_coms[i][j] == 'D':
pos.append([pos[-1][0] + 1, pos[-1][1]])
if ghost_coms[i][j] == 'R':
pos.append([pos[-1][0], pos[-1][1] + 1])
if ghost_coms[i][j] == 'L':
pos.append([pos[-1][0], pos[-1][1] - 1])
for j in range(len(ghost_coms[i])-2, 0, -1):
pos.append(pos[j])
ans.append(pos)
return ans
def shortest_dist_two_points_upd(start, end, mapa, ghost_maps):
explored = []
queue = [[start]]
while queue:
path = queue.pop(0)
u = path[-1]
max_len = len(ghost_maps[0])
step = (len(path))%max_len
act_ghosts = [line[step] for line in ghost_maps]
if u not in explored:
adj = []
if mapa[u[0]][u[1] + 1] != 'W' and [u[0], u[1] + 1] not in act_ghosts:
adj.append([u[0], u[1] + 1])
if mapa[u[0]][u[1] - 1] != 'W' and [u[0],u[1] - 1] not in act_ghosts:
adj.append([u[0], u[1] - 1])
if mapa[u[0] + 1][u[1]] != 'W' and [u[0] + 1,u[1]] not in act_ghosts:
adj.append([u[0] + 1, u[1]])
if mapa[u[0] - 1][u[1]] != 'W' and [u[0] - 1,u[1]] not in act_ghosts:
adj.append([u[0] - 1, u[1]])
for neighbour in adj:
new_path = list(path)
new_path.append(neighbour)
queue.append(new_path)
if neighbour == end:
return new_path
explored.append(u)
return
def pacman5():
for m in range(1, 6):
f = open('data/level5_' + str(m) + '.in', 'r')
# f = open('data/level5_example.in', 'r')
n = int(f.readline())
mapa = []
counter = 0
for _ in range(n):
mapa.append(f.readline())
y, x = map(int, f.readline().split())
x -= 1
y -= 1
for i in range(n):
for j in range(n):
if mapa[i][j] == 'C':
coin = [i, j]
ghost_pos = []
ghost_coms = []
ghosts = int(f.readline())
for _ in range(ghosts):
g_y, g_x = map(int, f.readline().split())
g_y -= 1
g_x -= 1
ghost_pos.append([g_y, g_x])
_ = int(f.readline())
ghost_coms.append(f.readline())
tmp = shortest_dist_two_points_upd([y,x], coin, mapa, ghost_positions(ghosts, ghost_pos, ghost_coms))
aws_tab = decode(tmp, mapa)
ans = ''
for el in aws_tab:
ans += el
print(ans)
out = open('data/level5_' + str(m) + '.out', 'w')
out.write(str(ans))
out.close()
print(ans)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
pacman5()