-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique_paths.cpp
More file actions
29 lines (27 loc) · 880 Bytes
/
unique_paths.cpp
File metadata and controls
29 lines (27 loc) · 880 Bytes
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
#include <iostream>
#include <vector>
int uniquePath(const std::vector<std::vector<int> >& grid) {
std::vector<std::vector<int> > dp(grid.size(), std::vector<int>(grid[0].size()));
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[0].size(); j++) {
if (grid[i][j] == 0) {
if (i == 0 && j == 0) dp[i][j] = 1;
else if (i == 0) dp[i][j] = dp[i][j - 1] > 0 ? 1 : 0;
else if (j == 0) dp[i][j] = dp[i - 1][j] > 0 ? 1 : 0;
else {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
}
return dp[grid.size() - 1][grid[0].size() - 1];
}
int main() {
std::vector<std::vector<int> > grid = {
{0,0,0},
{0,1,0},
{0,0,0}
};
std::cout << uniquePath(grid) << std::endl;
return 0;
}