链接:
思路:
对于有障碍的节点map[i][j]=1,dp[i][j]=0。对于无障碍节点map[i][j]=0,若map[i-1][j]=0,则dp[i][j]+=dp[i-1][j];若map[i][j-1]=0,则dp[i][j]+=dp[i][j-1];
实现:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid) {
int m = obstacleGrid.size();
vector<int> row = obstacleGrid.at(0);
int n = row.size();
int path[110][110];
memset(path, 0, sizeof(path));
int cnt = 0;
if (obstacleGrid[0][0] == 0) {
path[0][0] = 1;
}
for (int i = 1; i < m; i++) {
if (obstacleGrid[i][0] == 1) {
path[i][0] = 0;
continue;
}
if (obstacleGrid[i - 1][0] == 0) {
path[i][0] += path[i - 1][0];
}
}
for (int j = 1; j < n; j++) {
if (obstacleGrid[0][j] == 1) {
path[0][j] = 0;
continue;
}
if (obstacleGrid[0][j - 1] == 0) {
path[0][j] += path[0][j - 1];
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (obstacleGrid[i][j] == 1) {
path[i][j] = 0;
continue;
}
if (obstacleGrid.at(i - 1).at(j) == 0) {
path[i][j] += path[i - 1][j];
}
if (obstacleGrid.at(i).at(j - 1) == 0) {
path[i][j] += path[i][j - 1];
}
}
}
return path[m - 1][n - 1];
}
};
网友评论