力扣 题目63- 不同路径II
题目
题解
基本思想在62题 我们只要让障碍物在的格子取0即可
代码
1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 class Solution { 6 public: 7 int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { 8 int m = obstacleGrid.size(); 9 int n = obstacleGrid[0].size(); 10 vector<vector<int>> dp(m, vector<int>(n, 0)); 11 //一列 所以必定只有一条 路线 12 for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1; 13 //一行 所以必定只有一条 路线 14 for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0][j] = 1; 15 for (int i = 1; i < m; i++) { 16 for (int j = 1; j < n; j++) { 17 if (obstacleGrid[i][j] == 1) continue; 18 dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 19 } 20 } 21 return dp[m - 1][n - 1]; 22 } 23 }; 24 25 int main() { 26 Solution sol; 27 vector<vector<int>> obstacleGrid = {{0,0,0} ,{0,1,0},{0,0,0},{0,0,0} }; 28 int result = sol.uniquePathsWithObstacles(obstacleGrid); 29 cout << result << endl; 30 }