-
Notifications
You must be signed in to change notification settings - Fork 481
/
0980.py
28 lines (26 loc) · 908 Bytes
/
0980.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
class Solution:
def uniquePathsIII(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
start, end, p = None, None, 1
row, col = len(grid), len(grid[0])
for i in range(row):
for j in range(col):
if grid[i][j] == 1:
start = i, j
elif grid[i][j] == 2:
end = i, j
elif grid[i][j] == 0:
p += 1
def dfs(x, y, p):
if not (0 <= x < row and 0 <= y < col and grid[x][y] >= 0):
return 0
if end == (x, y) and p == 0:
return 1
grid[x][y] = -1
res = dfs(x+1, y, p-1) + dfs(x, y+1, p-1) + dfs(x-1, y, p-1) + dfs(x, y-1, p-1)
grid[x][y] = 0
return res
return dfs(start[0], start[1], p)