-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path0980.cpp
52 lines (39 loc) · 1.14 KB
/
0980.cpp
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
class Solution {
int result = 0;
int empty = 1;
public:
void dfs(vector<vector<int>>& grid, int i, int j, int pos){
if(i < 0 || j < 0
|| i >= grid.size()
|| j >= grid[0].size()
|| grid[i][j] == -1)
return;
if(grid[i][j] == 2){
if(pos == empty)
result++;
return;
}
grid[i][j] = -1;
dfs(grid, i - 1, j, pos + 1);
dfs(grid, i + 1, j, pos + 1);
dfs(grid, i, j - 1, pos + 1);
dfs(grid, i, j + 1, pos + 1);
grid[i][j] = 0;
}
int uniquePathsIII(vector<vector<int>>& grid) {
int row = 0;
int col = 0;
for(int i = 0; i < grid.size(); i++){
for(int j = 0; j < grid[0].size(); j++){
if(grid[i][j] == 1){
row = i;
col = j;
}
else if(grid[i][j] == 0)
empty++;
}
}
dfs(grid, row, col, 0);
return result;
}
};