-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path980. Unique Paths III 31 dec
54 lines (49 loc) · 1.1 KB
/
980. Unique Paths III 31 dec
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
class Solution {
public:
int ans=0;
void funs(vector<vector<int>>grid,int i,int j,int count)
{
if(i<0 || j<0 || i>=grid.size() ||j>=grid[0].size())
{
return;
}
if(grid[i][j]==-1 || grid[i][j]==3)
{
return;
}
if(grid[i][j]==2 && count==0)
{
ans++;
return;
}
if(grid[i][j]==2)
{
return;
}
grid[i][j]=3;
funs(grid,i,j+1,count-1);
funs(grid,i+1,j,count-1);
funs(grid,i-1,j,count-1);
funs(grid,i,j-1,count-1);
}
int uniquePathsIII(vector<vector<int>>& grid) {
int x,y,count=0;
for(int i=0;i<grid.size();i++)
{
for(int j=0;j<grid[0].size();j++)
{
if(grid[i][j]==1)
{
x=i;
y=j;
}
if(grid[i][j]==0)
{
count++;
}
}
}
funs(grid,x,y,count+1);
return ans;
}
};