-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy paththe-maze-ii.py
More file actions
64 lines (54 loc) · 2.24 KB
/
the-maze-ii.py
File metadata and controls
64 lines (54 loc) · 2.24 KB
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
from collections import deque
from typing import Deque, Iterator
class Solution:
def shortestDistance(
self, maze: list[list[int]], start: list[int], destination: list[int]
) -> int:
rows, cols = len(maze), len(maze[0])
visited: set[tuple[int, int, int, int]] = set()
def hit_the_wall(row: int, col: int, row_dir: int, col_dir: int) -> bool:
return not (
0 <= row + row_dir < rows
and 0 <= col + col_dir < cols
and maze[row + row_dir][col + col_dir] == 0
)
def neighbours(row: int, col: int) -> Iterator[tuple[int, int, int, int]]:
for next_row, next_col in (
(row - 1, col),
(row + 1, col),
(row, col - 1),
(row, col + 1),
):
if (
0 <= next_row < rows
and 0 <= next_col < cols
and maze[next_row][next_col] == 0
):
yield (next_row, next_col, next_row - row, next_col - col)
queue: Deque[tuple[int, int, int, int, int]] = (
deque()
) # row, col, row_direction, col_direction, distance
for row_dir, col_dir in (
(0, 1),
(0, -1),
(1, 0),
(-1, 0),
):
queue.append((start[0], start[1], row_dir, col_dir, 0))
visited.add((start[0], start[1], row_dir, col_dir))
while queue:
row, col, row_dir, col_dir, distance = queue.popleft()
if hit_the_wall(row, col, row_dir, col_dir) and [row, col] == destination:
return distance
for next_row, next_col, next_row_dir, next_col_dir in (
neighbours(row, col)
if hit_the_wall(row, col, row_dir, col_dir)
else ((row + row_dir, col + col_dir, row_dir, col_dir),)
):
if (next_row, next_col, next_row_dir, next_col_dir) in visited:
continue
visited.add((next_row, next_col, next_row_dir, next_col_dir))
queue.append(
(next_row, next_col, next_row_dir, next_col_dir, distance + 1)
)
return -1