Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?

网格中的障碍物和空位置分别用 1 和 0 来表示。
说明:m 和 n 的值均不超过 100。
示例 1:
输入:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
输出: 2
解释:
3x3 网格的正中间有一个障碍物。
从左上角到右下角一共有2条不同的路径:
1. 向右 -> 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右 -> 向右
定义dp[i][j]等于从起点开始到i,j的路径数
状态转移方程:dp[i][j] = dp[i-1][j] + dp[i][j-1]; 这题较62题来说,需要注意几个点:
1、图中有障碍物,障碍物处的dp[i][j]=0
2、一开始初始化第一行和第一列的dp[i][j]的时候,如果有一个点有障碍物,那么其后面的点都不能通过
举例:
[
[0,1,0],
[1,0,0],
[0,0,0]
]
这样的一个地图,初始化第一行的dp时,dp[0] = [1,0,0];
初始化第一列的dp时,dp[][1] = [1,0,0];
3、如果起点是障碍物,那么不能通过,即有0条通过的路径
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if (obstacleGrid == null) return 0;
if (obstacleGrid[0][0] == 1) return 0;
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
dp[0][0] = 1;
for (int i = 1; i < n; i++) {
if (obstacleGrid[0][i] == 0) {
dp[0][i] = dp[0][i - 1];
}
}
for (int i = 1; i < m; i++) {
if (obstacleGrid[i][0] == 0) {
dp[i][0] = dp[i - 1][0];
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if(obstacleGrid[i][j]==0) dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
}
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)的更多相关文章
- Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)
Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...
- [Swift]LeetCode63. 不同路径 II | Unique Paths II
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- [LeetCode] 63. Unique Paths II 不同的路径之二
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
随机推荐
- Web前端开发——HTML概述
HTML HyperText MakeUp Language,“超文本标记语言”,它是制作网页的标准语言 超文本就是通过链接的方式将文本有机地组织在一起,HTML的标记称为标签. 标签 HTML由标 ...
- Mac修改显示器使支持原生缩放
教程 直接搬运没有意义,直接放链接.地址:https://bbs.feng.com/read-htm-tid-11677019.html 若无法访问请使用网页截图备份.地址:https://img20 ...
- php文件夹上传源码
1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...
- Confluence 6 在一个空间中查看所有附加的文件
有下面 2 种方法可以让你查看空间的所有附件.你可以: 使用 Space Attachments Macro 来在一个页面中显示列表文件. 进入空间后,然后从边栏的底部选择 空间工具(Space to ...
- CCPC-Wannafly & Comet OJ 夏季欢乐赛(2019)H
题面 被神葱安利安利了本题. 我们贪心的想,如果有那么一坨相等的学号,那么肯定是保留一个人学号不变,其余的再推到学号+1的位置(准备与那个位置的其他人合并)处理. 虽然a[i]可大至1e18,不过如果 ...
- python内置模块(time模块)
常用的python内置模块 一.time模块 在python的三种时间表现形式: 1.时间戳,给电脑看的. - 自1970-01-01 00:00:00到当前时间,按秒计算,计算了多少秒. impor ...
- Complete Tripartite
D - Complete Tripartite 思路:这个题是个染色问题.理解题意就差不多写出来一半了.开始的时候还想用离散化来储存每个点的状态,即它连接的点有哪些,但很无奈,点太多了,long lo ...
- python-获取程序的路径
python获取当前路径 import os,sys 使用sys.path[0].sys.argv[0].os.getcwd().os.path.abspath(file).os.path.realp ...
- java 根据html模板生成html文件
1.代码部分 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test. ...
- IDEA项目里Maven 的Plugins出现红线的解决方法
1.删除项目里的libraries(快捷键ctrl+shift+alt+s):Project Settings->Libraries,全选删除 2.删除之前项目产生的target 3.然后再in ...