-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path980. Unique Paths III.cpp
More file actions
56 lines (50 loc) · 1.27 KB
/
980. Unique Paths III.cpp
File metadata and controls
56 lines (50 loc) · 1.27 KB
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
int xM[4] = {-1,0,1,0};
int yM[4] = {0,1,0,-1};
class Solution {
vector<vector<int>> mgrid;
int row, col;
int count;
int tsteps;
public:
bool isValid(int x, int y){
if(x >= 0 && x <= row-1 && y >= 0 && y <= col-1 && mgrid[x][y] != -1)
return true;
return false;
}
void process(int x, int y, int step){
if(!isValid(x,y))
return;
if(mgrid[x][y] == 2){
if(step+1 == tsteps)
count++;
return;
}
mgrid[x][y] = -1;
for(int i = 0; i < 4; i++){
int nx = x + xM[i];
int ny = y + yM[i];
process(nx, ny, step+1);
}
mgrid[x][y] = 0;
}
int uniquePathsIII(vector<vector<int>>& grid) {
row = grid.size();
col = grid[0].size();
mgrid = move(grid);
count = 0;
int x, y;
tsteps = 0;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(mgrid[i][j] == 1){
x = i;
y = j;
}
if(mgrid[i][j] != -1)
tsteps++;
}
}
process(x, y, 0);
return count;
}
};