-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathUnique Paths III.java
48 lines (46 loc) · 1.39 KB
/
Unique Paths III.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
class Solution {
int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
public int uniquePathsIII(int[][] grid) {
if (grid.length == 0 || grid[0].length == 0) {
return 0;
}
int rows = grid.length;
int cols = grid[0].length;
int startX = 0;
int startY = 0;
int numOfEmptySquare = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1) {
startX = i;
startY = j;
}
if (grid[i][j] == 0) {
numOfEmptySquare++;
}
}
}
int[] count = {0};
helper(grid, startX, startY, numOfEmptySquare, 0, new boolean[rows][cols], count);
return count[0];
}
private void helper(
int[][] grid, int startX, int startY, int numOfEmptySquare,
int currEmptySquare, boolean[][] visited, int[] count
) {
if (startX < 0 || startX >= grid.length || startY < 0 || startY >= grid[0].length || grid[startX][startY] == -1 || visited[startX][startY]) {
return;
}
if (grid[startX][startY] == 2) {
if (currEmptySquare == numOfEmptySquare) {
count[0]++;
}
return;
}
visited[startX][startY] = true;
for (int[] dir : dirs) {
helper(grid, startX + dir[0], startY + dir[1], numOfEmptySquare, (grid[startX][startY] == 0 ? currEmptySquare + 1 : currEmptySquare), visited, count);
}
visited[startX][startY] = false;
}
}