-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path63.unique-paths-ii.cpp
60 lines (49 loc) · 1.37 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
56
57
58
59
/*
* @lc app=leetcode id=63 lang=cpp
*
* [63] Unique Paths II
*/
// @lc code=start
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
// #define DEBUG
using namespace std;
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
int dp[m][n];
memset(dp, 0, sizeof(int) * m * n);
if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) return 0;
dp[0][0] = 1;
for (int y = 0; y < m; y++) {
for (int x = 0; x < n; x++) {
int left = x == 0 || obstacleGrid[y][x-1] == 1 ? 0 : dp[y][x-1];
int top = y == 0 || obstacleGrid[y-1][x] == 1 ? 0 : dp[y-1][x];
if (x == 0 && y == 0) {
dp[0][0] = 1;
continue;
}
dp[y][x] = obstacleGrid[y][x] == 1 ? 0 : left + top;
}
}
for (int y = 0; y < m; y++) {
for (int x = 0; x < n; x++) {
#ifdef DEBUG
cout << dp[y][x] << ", ";
#endif
}
#ifdef DEBUG
cout << "\n";
#endif
}
return dp[m - 1][n - 1];
}
};
// @lc code=end