Leetcode dynamic programming 63. Different paths II

Title: A robot is located in the upper left corner of an mxn grid (the starting point is marked as "Start" in the figure below).

The robot can only move one step down or to the right at a time. The robot tries to reach the bottom right corner of the grid (labeled "Finish" in the image below).

Now consider that there are obstacles in the grid. How many different paths will there be from the upper left corner to the lower right corner?

Obstacles and empty positions in the grid are represented by 1 and 0 respectively.
Note: The values ​​of m and n do not exceed 100.
Example 1:
Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is an obstacle in the middle of the 3x3 grid.
There are 2 different paths from the upper left corner to the lower right corner:

  1. Right -> Right -> Down -> Down
  2. Down -> Down -> Right -> Right

Source: LeetCode
Link: https://leetcode-cn.com/problems/unique-paths-ii
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

code show as below

class Solution {
    
    
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    
    
        int m = obstacleGrid.length;
        int n= obstacleGrid[0].length;
        if(obstacleGrid[0][0]==1||obstacleGrid[m-1][n-1]==1)
           return 0;
        int[][]dp = new int[m][n];
        dp[0][0]=1;
        for(int i=0;i<m;i++){
    
    
            for(int j=0;j<n;j++){
    
    
                if(i==0&&j==0){
    
    
                    continue;
                }else if(i==0){
    
    
                    if(obstacleGrid[i][j-1]!=1){
    
    
                        dp[i][j]=dp[i][j-1];
                    }else{
    
    
                        continue;
                    }
                }else if(j==0){
    
    
                    if(obstacleGrid[i-1][j]!=1){
    
    
                        dp[i][j]=dp[i-1][j];
                    }else{
    
    
                        continue;
                    }
                }else{
    
    
                    if(obstacleGrid[i-1][j]==1&&obstacleGrid[i][j-1]==1){
    
    
                        continue;   
                    }else if(obstacleGrid[i-1][j]==1){
    
    
                        dp[i][j]=dp[i][j-1];
                    }else if(obstacleGrid[i][j-1]==1){
    
    
                        dp[i][j]=dp[i-1][j];
                    }else{
    
    
                        dp[i][j]=dp[i][j-1]+dp[i-1][j];
                    }
                }
            }
        }
        return dp[m-1][n-1];
    }
}

According to dynamic programming, we find the transition equation as dp[i][j] = dp[i][j-1]+dp[i-1][j];
for the first row and first column dp[i][ j]=dp[i-1][j] or dp[i][j-1] In addition, we need to determine whether there is an obstacle in the corresponding position. If there is, skip the value of the position, and finally get the dp array The last element is the result

Guess you like

Origin blog.csdn.net/qq_45789385/article/details/103464830