-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnearest-exit-from-entrance-in-maze.py
102 lines (95 loc) · 3.42 KB
/
nearest-exit-from-entrance-in-maze.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
# 1926. Nearest Exit from Entrance in Maze
# 🟠 Medium
#
# https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/
#
# Tags: Array - Breadth-First Search - Matrix
import timeit
from collections import deque
from typing import List
# Use BFS to visit cells one level at a time from the entrance, if we
# detect an exit, return the number of steps up to that point, if we
# run out of cells to visit and cannot detect an exit, return -1.
#
# Time complexity: O(m*n) - We will visit each cell once, there are m
# rows and n columns.
# Space complexity: O(m*n) - We could end up adding n/2 cells to the
# queue.
#
# Runtime: 749 ms, faster than 97.63%
# Memory Usage: 14.7 MB, less than 55.57%
class BFS:
def nearestExit(self, maze: List[List[str]], entrance: List[int]) -> int:
# Use BFS, count the steps needed.
q = deque([entrance])
# Mark cells we visit as visited.
maze[entrance[0]][entrance[1]] = "+"
steps = 0
while q:
steps += 1
# Pop an entire level.
for _ in range(len(q)):
r, c = q.popleft()
# Try to move in the four directions from this cell.
for i, j in ((r + 1, c), (r - 1, c), (r, c - 1), (r, c + 1)):
if (
0 <= i < len(maze)
and 0 <= j < len(maze[0])
and maze[i][j] == "."
):
# Check if this is an exit.
if (
i == 0
or j == 0
or i == len(maze) - 1
or j == len(maze[0]) - 1
):
return steps
# If not an exit, push it into the queue and mark
# visited, if we use the number of steps, it
# improves visualization on the debugger.
maze[i][j] = steps # "+"
q.append((i, j))
# If we cannot arrive at any exit, return -1
return -1
def test():
executors = [
BFS,
]
tests = [
# [[[".", "+"]], [0, 0], -1],
# [[["+", "+", "+"], [".", ".", "."], ["+", "+", "+"]], [1, 0], 2],
# [
# [["+", "+", ".", "+"], [".", ".", ".", "+"], ["+", "+", "+", "."]],
# [1, 2],
# 1,
# ],
[
[
["+", ".", "+", "+", "+", "+", "+"],
["+", ".", "+", ".", ".", ".", "+"],
["+", ".", "+", ".", "+", ".", "+"],
["+", ".", ".", ".", ".", ".", "+"],
["+", "+", "+", "+", ".", "+", "."],
],
[0, 1],
7,
],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.nearestExit(t[0], t[1])
exp = t[2]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()