63. 不同路径 II(dp)(Leetcode)
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector<vector<int>> ans(m, vector<int>(n));
if(obstacleGrid[0][0]) return 0;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(!obstacleGrid[i][j]){
if(!i && !j) ans[i][j] = 1;
else{
if(i) ans[i][j] += ans[i-1][j];
if(j) ans[i][j] += ans[i][j-1];
}
}
}
}
return ans[m-1][n-1];
}
};

浙公网安备 33010602011771号