-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path980. Unique Paths III
46 lines (45 loc) · 1006 Bytes
/
980. Unique Paths III
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
class Solution {
public:
int stx;
int sty;
int edx;
int edy;
int pathse;
int m;
int n;
int ans=0;
void dfs(vector<vector<int>> grid,int i,int j, int path){
if(i<0 || j<0 || i>=m || j>=n || grid[i][j]==-1)return ;
if(i==edx && j==edy && path==pathse){
ans++;
return ;
}
grid[i][j]=-1;
dfs(grid,i+1,j,path+1);
dfs(grid,i-1,j,path+1);
dfs(grid,i,j+1,path+1);
dfs(grid,i,j-1,path+1);
}
int uniquePathsIII(vector<vector<int>>& grid) {
m=grid.size();
n=grid[0].size();
pathse=m*n;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==1){
stx=i;
sty=j;
}
else if(grid[i][j]==2){
edx=i;
edy=j;
}
else if(grid[i][j]==-1){
pathse--;
}
}
}
dfs(grid,stx,sty,1);
return ans;
}
};