leetcode #63 in cpp

原创 2016年05月30日 22:51:08

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]

The total number of unique paths is 2.



Solution:

It is almost the same as #62. In this question we have to check if a position(i,j) has obstacle. If it has one, leave dp[i][j] as 0 as it has no paths to destination. 

Code:

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        if(obstacleGrid[m-1][n-1] == 1) return 0;//if destination has obstacle, it can never be reached
        vector<vector<int>> dp(m, vector<int>(n,0));
        dp[m-1][n-1] = 1;//set destination be 1
        for(int i = m-2; i >=0 && n-1>=0; i --){//right border
            if(obstacleGrid[i][n-1]!=1) dp[i][n-1] = dp[i+1][n-1];//if a position has obstacle, the cell above it would have path 0 to destination.
        }
        for(int i = n-2; i >=0 && m-1 >= 0; i--){//bottom row
            if(obstacleGrid[m-1][i]!=1) dp[m-1][i] = dp[m-1][i+1];//if a position has obstacle, the cells at its left would have path 0 to destination.
        }
        for(int i = m-2; i >= 0; i --){
            for(int j = n-2; j >= 0; j --){
                //if it is obstacle, skip it and leave it as 0. There are no paths from obstacle to destination
                //if not obstacle, then we could calculate the number of paths. 
                if(obstacleGrid[i][j] != 1) dp[i][j] += dp[i+1][j] + dp[i][j+1];
            }
        }
        return dp[0][0];
    }
};


相关文章推荐

leetcode #36 in cpp.

The question is to determine a Sudoku is valid.  Solution:  if a Sudoku is valid, each row, each co...

*leetcode 138 in cpp

A linked list is given such that each node contains an additional random pointer which could point t...

leetcode #17 in cpp

The question is : Given a digit string, return all possible letter combinations that the number coul...

leetcode $42 in cpp

Code: (This method is not the best one. There are other methods using two pointers) Key: a bar coul...

*leetcode #80 in cpp

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, G...

Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)

Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月27日 02:35
  • 213

Leetcode 25. Reverse Nodes in k-Group (Hard) (cpp)

Leetcode 25. Reverse Nodes in k-Group (Hard) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月10日 04:04
  • 158

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年08月23日 11:17
  • 274

leetcode #60 in cpp

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...

Leetcode #91 in cpp

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #63 in cpp
举报原因:
原因补充:

(最多只允许输入30个字)