-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18-01.py
27 lines (27 loc) · 909 Bytes
/
18-01.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
from collections import deque
inputval = ""
falling_bytes = []
while True:
inputval = input()
if not inputval:
break
falling_bytes.append(tuple(map(int, inputval.split(","))))
visited = {
(0, 0): 0
}
bfs_queue = deque()
bfs_queue.append((0, 0))
obstacles = set(falling_bytes[:min(len(falling_bytes), 1024)])
dirs = [(-1, 0), (0, 1), (1, 0), (0, -1)]
mapsize = 70
while bfs_queue:
row, col = bfs_queue.popleft()
for drow, dcol in dirs:
nrow, ncol = row + drow, col + dcol
if nrow >= 0 and nrow <= mapsize and ncol >= 0 and ncol <= mapsize:
if (nrow, ncol) not in obstacles and (nrow, ncol) not in visited:
visited[(nrow, ncol)] = visited[(row, col)] + 1
bfs_queue.append((nrow, ncol))
if nrow == mapsize and ncol == mapsize:
print(visited[(nrow, ncol)])
exit(0)