-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path980.go
More file actions
40 lines (36 loc) · 903 Bytes
/
Copy path980.go
File metadata and controls
40 lines (36 loc) · 903 Bytes
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
package main
func uniquePathsIII(grid [][]int) int {
startX := 0
startY := 0
num := 0
for i := 0; i < len(grid); i ++ {
for j := 0; j < len(grid[0]); j ++ {
if (grid[i][j] == 1) {
startX = i
startY = j
} else if grid[i][j] != -1 {
num ++
}
}
}
return dfs(grid, startX, startY, num)
}
func dfs(grid [][]int, x int, y int, num int) int {
if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) || grid[x][y] == -1 {
return 0
}
if grid[x][y] == 2 {
if num == 0 {
return 1
}
return 0
}
grid[x][y] = -1
ret := 0
ret += dfs(grid, x, y - 1, num - 1)
ret += dfs(grid, x, y + 1, num - 1)
ret += dfs(grid, x - 1, y, num - 1)
ret += dfs(grid, x + 1, y, num - 1)
grid[x][y] = 0
return ret
}