-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy path490.py
More file actions
18 lines (18 loc) · 674 Bytes
/
490.py
File metadata and controls
18 lines (18 loc) · 674 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def hasPath(self, maze, start, destination):
m, n, stopped = len(maze), len(maze[0]), set()
def dfs(x, y):
if (x, y) in stopped:
return False
stopped.add((x, y))
if [x, y] == destination:
return True
for i, j in (-1, 0) , (1, 0), (0, -1), (0, 1):
newX, newY = x, y
while 0 <= newX + i < m and 0 <= newY + j < n and maze[newX + i][newY + j] != 1:
newX += i
newY += j
if dfs(newX, newY):
return True
return False
return dfs(*start)