Algorithm-Different Path Problem

1. Different paths (I)

62. Different paths

This problem is a relatively classic problem of dynamic programming, which is essentially the same as the problem of stepping, except that the position of the jump is different.
Insert picture description here

	一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
	机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
	问总共有多少条不同的路径?

例如,上图是一个7 x 3 的网格。有多少可能的路径?

示例 1:

输入: m = 3, n = 2
输出: 3
解释:
从左上角开始,总共有 3 条路径可以到达右下角。
1. 向右 -> 向右 -> 向下
2. 向右 -> 向下 -> 向右
3. 向下 -> 向右 -> 向右
示例 2:

输入: m = 7, n = 3
输出: 28
 

提示:

1 <= m, n <= 100
题目数据保证答案小于等于 2 * 10 ^ 9

1.1 Preliminary solution

Since it can only go right or down, there will be such a relationship when the robot passes a certain point

dp[i][j]=dp[i-1][j]+dp[i][j-1];

That is, the number of plans at the current position is the sum of the number of plans on the left and the number of plans on the upper side.

How do we determine the initial conditions?

Because this robot walks uniquely, we can actually walk it to each position in the first row or column, and there is only one solution: one way to the black. For this, we can initialize it to 1

public int uniquePaths(int m, int n) {
    
    
	int[][]dp=new int[m][n];//表示到达每个位置的方案数
	for(int i=0;i<m;i++){
    
    
		dp[m][0]=1;
	}
	for(int i=0;i<n;i++){
    
    
		dp[0][n]=1;
	}
	for(int i=1;i<m;i++){
    
    
		for(int j=1;j<n;j++){
    
    
			dp[i][j]=dp[i-1][j]+dp[i][j-1];
		}
	}
	return dp[m-1][n-1];
}

We can further optimize it,

1.2 Time optimization

We used two loops to initialize the dp array. In fact, this is unnecessary. We only need to determine whether the access is the left and upper boundaries when traversing the matrix, and set the left and upper boundaries to 1.

    public int uniquePaths(int m, int n) {
    
    
        if(m==1||n==1){
    
    
            return 1;
        }
        int[][]dp=new int[m][n];//表示到达每个位置的方案数
        for(int i=1;i<m;i++){
    
    
            for(int j=1;j<n;j++){
    
    
                if(i-1==0){
    
    
                    dp[0][j]=1;
                }
                if(j-1==0){
    
    
                    dp[i][0]=1;
                }
                dp[i][j]=dp[i-1][j]+dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }

2. Different paths (II)

63. Different paths (||)

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。

现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?



网格中的障碍物和空位置分别用 1 和 0 来表示。

说明:m 和 n 的值均不超过 100。

示例 1:

输入:
[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
输出: 2
解释:
3x3 网格的正中间有一个障碍物。
从左上角到右下角一共有 2 条不同的路径:
1. 向右 -> 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右 -> 向右

There were obstacles in our path this time, but it was not a problem.

The transfer equation is still the above

dp[i][j]=dp[i-1][j]+dp[i][j-1];

But we need to add a condition to the obstacle. If the position of i, j is an obstacle, then we mark dp[i][j] as 0, and that's it.


    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    
    
    	int m=obstacleGrid.length,n=obstacleGrid[0].length;
		int[][]dp=new int[m][n];//表示到达每个位置的方案数
		for(int i=0;i<m&&(obstacleGrid[i][0]!=1);i++){
    
    
			dp[i][0]=1;
		}
		for(int i=0;i<n&&(obstacleGrid[0][i]!=1);i++){
    
    
			dp[0][i]=1;
		}
		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];
    }

The space complexity above can be optimized, but it is not very elegant to write.

Guess you like

Origin blog.csdn.net/qq_23594799/article/details/105422806