/
unique-paths-ii.cpp
58 lines (47 loc) · 1.23 KB
/
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
56
57
58
#include <vector>
#include <iostream>
using namespace std;
class Solution
{
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid)
{
if (!obstacleGrid.size()) {
return 0;
}
int n = obstacleGrid.size();
int m = obstacleGrid.front().size();
if (obstacleGrid[0][0] || obstacleGrid[n - 1][m - 1]) {
return 0;
}
vector<int> row(m, 0);
vector<vector<int> > f(n, row);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (i == 0 && j == 0) {
f[i][j] = 1;
continue;
}
f[i][j] = 0;
if (!obstacleGrid[i][j]) {
if (i > 0) {
f[i][j] += f[i - 1][j];
}
if (j > 0) {
f[i][j] += f[i][j - 1];
}
}
}
}
return f[n - 1][m - 1];
}
};
int main(int argc, char *argv[])
{
Solution solve;
vector<int> row(3, 0);
vector<vector<int> > grid(3, row);
grid[1][1] = 1;
cout << solve.uniquePathsWithObstacles(grid) << endl;
return 0;
}