[LeetCode] 63. Unique Paths II

原题链接: https://leetcode.com/problems/unique-paths-ii/

1. 题目介绍

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

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.
Note: m and n will be at most 100.

有一个长为m, 宽为n的格子区域。n和m最大是100。
这个格子区域左上角有一个机器人,右下角有一个星星。机器人只能向右或者向下走,并且有的格子还有障碍物,机器人无法通过有障碍物的格子。

问机器人到达星星的不同路线有多少条?
在这里插入图片描述
Example 1:

Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1 . Right -> Right -> Down -> Down
2 . Down -> Down -> Right -> Right

2. 解题思路

2.1 动态规划法

这个题是62. Unique Paths的进化版本。因此思路和上一题非常类似,也是使用动态规划的思路。

构造二维数组dp,遍历一遍数组dp,递推得到最优解。这里和62. Unique Paths不同的是加入了障碍物,障碍物在dp数组中对应的位置是0,代表无法经过此处通往终点。

递推公式:
d p [ i ] [ j ] = d p [ i ] [ j + 1 ] + d p [ i + 1 ] [ j ] dp[i][j] = dp[i][j+1] + dp[i+1][j] dp[i][j]=dp[i][j+1]+dp[i+1][j]

这道题的部分特殊测试样例需要注意一下,比如:

  1. 起点、终点、障碍物三者都在同一个位置
    在这里插入图片描述
  2. 起点和终点在同一个位置,而且这个位置没有障碍物
    在这里插入图片描述
  3. 在终点有障碍物,也即无论怎么走都无法到达终点。
    在这里插入图片描述
    实现代码
class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
		
		int length = obstacleGrid.length;
		int width = obstacleGrid[0].length;
		
		if(length ==1 || width == 1 ) {
		//如果有一个边长为1,只要出现了障碍物,就一定无法走到终点
		//如果没有障碍物,那么走到终点的路线必定只有1条
			for(int i = 0;i<length;i++) {
				for(int j = 0;j<width;j++) {
					if(obstacleGrid[i][j] == 1) {
						return 0;
						
					}
				}
			}
			return 1;
		}
		
		if(obstacleGrid[length-1][width-1] ==1) {
			return 0;
			//如果终点有障碍物,必须返回0
		}
		
        int[][] dp = new int[length+1][width+1];
        if(obstacleGrid[length-1][width-2] == 0 ) {
        	dp[length-1][width-2] =1;
        }
        if(obstacleGrid[length-2][width-1] == 0 ) {
        	dp[length-2][width-1] =1;
        }

        for(int i = length-1 ; i>=0 ; i--) {
        	for(int j = width-1 ; j>=0 ; j--) {
        		if(obstacleGrid[i][j] == 1) {
        			dp[i][j] = 0;
        		}
        		else {
        			if(dp[i][j] != 0) {
            			continue;
            		}
            		dp[i][j] = dp[i][j+1] + dp[i+1][j];
        		}
        	}
        }
        return dp[0][0];
    }
}

2.2 进一步改进

二维数组可以简化为一维数组。因为在递推关系式中,
d p [ i ] [ j ] = d p [ i ] [ j + 1 ] + d p [ i + 1 ] [ j ] dp[i][j] = dp[i][j+1] + dp[i+1][j] dp[i][j]=dp[i][j+1]+dp[i+1][j]
更新 dp[ i ][ j ] 只用到了同行上一列和同列上一行的数值,因此可以简化为一维数组。

实现代码

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
		
		int length = obstacleGrid.length;
		int width = obstacleGrid[0].length;
		
		if(length ==1 || width == 1 ) {
			//如果有一个边长为1,只要出现了障碍物,就一定无法走到终点
			//如果没有障碍物,那么走到终点的路线必定只有1条
			for(int i = 0;i<length;i++) {
				for(int j = 0;j<width;j++) {
					if(obstacleGrid[i][j] == 1) {
						return 0;
					}
				}
			}
			return 1;
		}
		
		if(obstacleGrid[length-1][width-1] ==1) {
			//如果终点有障碍物,必须返回0
			return 0;
		}
		
		int [] dp = new int[width+1];
		dp[width-1] = 1;
       
        for(int i = length-1 ; i>=0 ; i--) {
        	for(int j = width-1 ; j>=0 ; j--) {
        		if(obstacleGrid[i][j] == 1) {
        			dp[j] = 0;
        		}
        		else {
            		dp[j] += dp[j+1] ;
        		}
        	}
        }
        return dp[0];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值