Rico-63 Different Paths II(dp)

Leek-63 Different Paths II

1. Topic

A robot is located in the upper left corner of a m x ngrid (the starting point is labeled "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 (marked "Finish" in the image below).

Now consider that there are obstacles in the grid. So how many different paths will there be from top left to bottom right?

Obstacles and empty positions in the 1grid 0are denoted by and , respectively.

Example 1:

img

Input: obstacleGrid = [[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

2. Analysis

1. Problem. After reading through the questions, I found that the only difference from the previous 62 different paths is that there is an additional obstacle. The general approach is the same as the previous 62 questions, and the questions are also 求从左下角到右下角不同路径quantitative.
2. Method. Cumulative summation also uses our dynamic programming dp , and recursion can also be used, but the time complexity of recursion is relatively high.
3. Initialize. The question stem can only go down or to the right, and there are obstacles, so when we are simulating, the first line can only have one path from the starting point. Once an obstacle is encountered, the latter cannot pass. Same for the first column. Therefore, the number of different paths in the grid can be set to 1 when there is no obstacle in front, and 0 if there is one.
4. When traversing the middle grid and encountering an obstacle , we can set its dp value to 0 , which means that no path can be obtained through it.
5. Then write the code.

3. Code and comments

class Solution {
    
    
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    
    
        // 1.dp[i][j] 表示到i行j列的格子时候总共不同路径的数量
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];

        // 2.设置初始值
        for (int i = 0; i < n; i++){
    
    
            if (obstacleGrid[0][i] == 0){
    
    
                dp[0][i] = 1;
            }else{
    
    
                break;
            }
        }
        for (int j = 0; j < m; j++){
    
    
            if (obstacleGrid[j][0] == 0){
    
    
                dp[j][0] = 1;
            }else{
    
    
                break;
            }
        }

        // 3.中间
        for (int i = 1; i < m; i++){
    
    
            for (int j = 1; j < n; j++){
    
    
                if (obstacleGrid[i][j] == 1){
    
    
                    dp[i][j] = 0;
                }else{
    
    
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 
                }
            }
        }
        return dp[m - 1][n - 1];
    }
}

4. Practice

Leetcode topic link: https://leetcode.cn/problems/unique-paths-ii/

Guess you like

Origin blog.csdn.net/qq_51326491/article/details/129245953