标签:
1 class Solution { 2 public: 3 int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { 4 if(obstacleGrid.size()==0 || obstacleGrid[0].size()==0) return 0; 5 int m = obstacleGrid.size(), n = obstacleGrid[0].size(); 6 if(obstacleGrid[m-1][n-1]==1) return 0; 7 8 vector<vector<int> > dp(m); 9 for(int i=0;i<dp.size();++i) dp[i].resize(n); 10 for(int i=0;i<dp.size();++i){ 11 for(int j=0;j<dp[i].size();++j) dp[i][j]=0; 12 } 13 14 dp[0][0] = (obstacleGrid[0][0]==1) ? 0 : 1; 15 for(int i=1;i<dp.size();++i){ 16 if(dp[i-1][0] && !obstacleGrid[i][0]) dp[i][0] = 1; 17 } 18 for(int j=1;j<dp[0].size();++j){ 19 if(dp[0][j-1] && !obstacleGrid[0][j]) dp[0][j] = 1; 20 } 21 for(int i=1;i<dp.size();++i){ 22 for(int j=1;j<dp[i].size();++j){ 23 if(i>=1 && !obstacleGrid[i-1][j]) dp[i][j] += dp[i-1][j]; 24 if(j>=1 && !obstacleGrid[i][j-1]) dp[i][j] += dp[i][j-1]; 25 } 26 } 27 28 return dp[m-1][n-1]; 29 } 30 };
leetcode@ [62/63] Unique Paths II
标签:
原文地址:http://www.cnblogs.com/fu11211129/p/4904542.html