Skip to content

Commit 77a0b6f

Browse files
committed
O(V+E) time and O(E) space using Deque and BFS.
1 parent 7c65c39 commit 77a0b6f

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

490. The Maze/490. The Maze_BFS.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int
99
if [i,j] == destination:
1010
return True
1111
for x,y in dirs:
12-
row = i + x
13-
col = j + y
14-
while 0 <= row < len(maze) and 0 <= col < len(maze[0]) and maze[row][col]!=1:
12+
row = i
13+
col = j
14+
while 0 <= row+x < len(maze) and 0 <= col+y < len(maze[0]) and maze[row+x][col+y]!=1:
1515
row += x
1616
col += y
17-
row -= x
18-
col -= y
1917
if maze[row][col] == 0:
2018
Q.append([row,col])
2119
return False

0 commit comments

Comments
 (0)