0063.cpp (784B)
1 class Solution { 2 public: 3 int uniquePathsWithObstacles(vector<vector<int>> &obstacleGrid) { 4 int dp[101][101] = {0}; 5 int n = obstacleGrid.size(), m = obstacleGrid[0].size(); 6 7 for (int i = 0, flag = 0; i < n; i++) { 8 if (flag || obstacleGrid[i][0]) 9 flag = true; 10 else 11 dp[i][0] = 1; 12 } 13 14 for (int i = 0, flag = 0; i < m; i++) { 15 if (flag || obstacleGrid[0][i]) 16 flag = true; 17 else 18 dp[0][i] = 1; 19 } 20 21 for (int i = 1; i < n; i++) { 22 for (int j = 1; j < m; j++) { 23 if (obstacleGrid[i][j] != 1) dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 24 } 25 } 26 return dp[n - 1][m - 1]; 27 } 28 };