Skip to content

Commit 94e529e

Browse files
committed
Time: 5 ms (57.49%), Space: 7.1 MB (64.25%) - LeetHub
1 parent 369aeb2 commit 94e529e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int res = 0, empty = 1;
4+
void dfs(vector<vector<int>>& grid, int x, int y, int count) {
5+
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] == -1) return;
6+
7+
if (grid[x][y] == 2) {
8+
if(empty == count) res++;
9+
return;
10+
}
11+
12+
grid[x][y] = -1;
13+
14+
dfs(grid, x+1, y, count+1);
15+
dfs(grid, x-1, y, count+1);
16+
dfs(grid, x, y+1, count+1);
17+
dfs(grid, x, y-1, count+1);
18+
19+
grid[x][y] = 0;
20+
21+
}
22+
23+
int uniquePathsIII(vector<vector<int>>& grid) {
24+
int start_x, start_y;
25+
for (int i = 0; i < grid.size(); i++) {
26+
for (int j = 0; j < grid[0].size(); j++) {
27+
if (grid[i][j] == 1) start_x = i, start_y = j;
28+
else if (grid[i][j] == 0) empty++;
29+
}
30+
}
31+
32+
dfs(grid, start_x, start_y, 0);
33+
return res;
34+
}
35+
};

0 commit comments

Comments
 (0)