LC.P980[不同路径III]

方法一:回溯

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
class Solution {

int[][] grid;
int m, n, cnt;
boolean[][] visited;
static int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

public int uniquePathsIII(int[][] grid) {
this.grid = grid;
m = grid.length;
n = grid[0].length;
visited = new boolean[m][n];
int x = 0, y = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 0) ++cnt;
else if (grid[i][j] == 1) {
x = i;
y = j;
}
}
}
visited[x][y] = true;
return dfs(x, y, 0);
}

/**
* 从 (x, y) 出发,且当前已经走过的单元格数量为 k 的路径数
*/
private int dfs(int x, int y, int k) {
if (grid[x][y] == 2) return k == cnt + 1 ? 1 : 0;
int ans = 0;
for (int[] dir : dirs) {
int nx = x + dir[0], ny = y + dir[1];
if (nx < 0 || nx >= m || ny < 0 || ny >= n || visited[nx][ny] || grid[nx][ny] == -1) continue;
visited[nx][ny] = true;
ans += dfs(nx, ny, k + 1);
visited[nx][ny] = false;
}
return ans;
}
}
  • 时间复杂度:$O(3^{mn})$
  • 空间复杂度:$O(m \times n)$