-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path63.unique-paths-ii.cpp
55 lines (42 loc) · 1.46 KB
/
63.unique-paths-ii.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
53
54
55
class Solution {
public:
int count = 0;
bool solve(vector<vector<int>>& obstacleGrid, int bottom, int right){
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
// cout<<"bottom : "<<bottom<<"\t"<<right<<endl;
if(bottom == m || right == n)
return true;
if(obstacleGrid[bottom][right])
return false;
if(bottom == m-1 && right == n-1){
count++;
return true;
}
solve(obstacleGrid, bottom, right+1);
solve(obstacleGrid, bottom+1, right);
return true;
}
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
// solve(obstacleGrid, 0,0);
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
int dp[m][n];
memset(dp, 0, sizeof(dp));
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(obstacleGrid[i][j])
continue;
if(i == 0 && j == 0)
dp[i][j] = 1;
else if(i==0)
dp[i][j] += dp[i][j-1];
else if(j==0)
dp[i][j] += dp[i-1][j];
else
dp[i][j] += dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
};