-
Notifications
You must be signed in to change notification settings - Fork 2
/
Solution.java
72 lines (60 loc) · 1.78 KB
/
Solution.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.github.datastructureandalgorithm.graph.chapter9;
/**
* LeetCode 980.不同路径 III:https://leetcode-cn.com/problems/unique-paths-iii/
*/
class Solution {
private int[][] grid;
private int r, c;
private boolean[][] visited;
private int left;
private int start;
private int end;
private int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
public int uniquePathsIII(int[][] grid) {
this.grid = grid;
this.r = grid.length;
this.c = grid[0].length;
this.visited = new boolean[r][c];
this.left = r * c;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
if (grid[i][j] == 1) {
start = i * c + j;
grid[i][j] = 0;
} else if (grid[i][j] == 2) {
end = i * c + j;
grid[i][j] = 0;
} else if (grid[i][j] == -1) {
left--;
}
return dfs(start);
}
int dfs(int s) {
int x = s / c;
int y = s % c;
visited[x][y] = true;
left--;
if (s == end && left == 0) {
// 回溯
visited[x][y] = false;
left++;
return 1;
}
int res = 0;
for (int d = 0; d < 4; d++) {
int nextX = x + dirs[d][0];
int nextY = y + dirs[d][1];
if (isValid(nextX, nextY) && !visited[nextX][nextY] && grid[nextX][nextY] != -1) {
int next = nextX * c + nextY;
res += dfs(next);
}
}
// 回溯
visited[x][y] = false;
left++;
return res;
}
private boolean isValid(int x, int y) {
return x >= 0 && x < r && y >= 0 && y < c;
}
}