当前位置:网站首页>动态规划——63. 不同路径 II
动态规划——63. 不同路径 II
2022-07-28 03:21:00 【向着百万年薪努力的小赵】
1 题目描述
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish”)。
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
网格中的障碍物和空位置分别用 1 和 0 来表示。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/unique-paths-ii
2 题目示例

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

输入:obstacleGrid = [[0,1],[0,0]]
输出:1
3 题目提示
m == obstacleGrid.length
n == obstacleGrid[i].length
1 <= m, n <= 100
obstacleGrid[i][j] 为 0 或 1
4 思路
这道题相对于62.不同路径 (opens new window)就是有了障碍。
第一次接触这种题目的同学可能会有点懵,这有障碍了,应该怎么算呢?
62.不同路径 (opens new window)中我们已经详细分析了没有障碍的情况,有障碍的话,其实就是标记对应的dp table(dp数组)保持初始值(0)就可以了。
动规五部曲:
确定dp数组(dp table)以及下标的含义
dp[i][j] :表示从(0 ,0)出发,到(i, j) 有dp[i][j]条不同的路径。
确定递推公式
递推公式和62.不同路径一样,dp[i][j] = dp[i - 1][j] + dp[i][j - 1]。
但这里需要注意一点,因为有了障碍,(i, j)如果就是障碍的话应该就保持初始状态(初始状态为0)。
所以代码为:
if (obstacleGrid[i][j] == 0) {
// 当(i, j)没有障碍的时候,再推导dp[i][j]
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
从递归公式dp[i][j] = dp[i - 1][j] + dp[i][j - 1] 中可以看出,一定是从左到右一层一层遍历,这样保证推导dp[i][j]的时候,dp[i - 1][j] 和 dp[i][j - 1]一定是有数值。
本题是62.不同路径 (opens new window)的障碍版,整体思路大体一致。
但就算是做过62.不同路径,在做本题也会有感觉遇到障碍无从下手。
其实只要考虑到,遇到障碍dp[i][j]保持0就可以了。
也有一些小细节,例如:初始化的部分,很容易忽略了障碍之后应该都是0的情况。
本题思路采自代码随想录
链接:https://programmercarl.com/0063.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84II.html#%E6%80%9D%E8%B7%AF
5 我的答案
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int n = obstacleGrid.length, m = obstacleGrid[0].length;
int[][] dp = new int[n][m];
for (int i = 0; i < m; i++) {
if (obstacleGrid[0][i] == 1) break; //一旦遇到障碍,后续都到不了
dp[0][i] = 1;
}
for (int i = 0; i < n; i++) {
if (obstacleGrid[i][0] == 1) break; 一旦遇到障碍,后续都到不了
dp[i][0] = 1;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (obstacleGrid[i][j] == 1) continue;
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[n - 1][m - 1];
}
}
边栏推荐
- How to reinstall win11 system with one click
- Win11输入法的选字框不见了怎么办?
- xctf攻防世界 Web高手进阶区 unserialize3
- Redis persistence mechanism
- Illustrated: detailed explanation of JVM memory layout
- C语言实现动态版本的通讯录
- Summary of redis classic interview questions
- After reading MySQL database advanced practice (SQL xiaoxuzhu)
- Redis implements distributed locks
- 版本兼容的问题
猜你喜欢

Malloc, free, calloc, realloc dynamic memory development functions in dynamic memory management

Shell: resource monitoring script and high load alarm
D2DEngine食用教程(4)———绘制文本

Summary of concurrent programming interview questions

Tensorboard usage record

Uniapp - make phone calls and send text messages

机器人开发--丝杠与导轨

Contour detection based on OpenCV (3)

每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。

Integrate SSM to realize search of addition, deletion, modification and query
随机推荐
The object array is converted to string and then separated by strings, including the competition to select the children of a field, or the sum,
golang gorm查询任意字段的组装方法
695. 岛屿的最大面积
容器相关的概念
xctf攻防世界 Web高手进阶区 PHP2
ES6 从入门到精通 # 09:Symbol 类型
8000字讲透OBSA原理与应用实践
Defect detection of BP SVM system design of leaf defect detection
2022最新Android Handler相关面试题总结
版本兼容的问题
动态内存管理中的malloc、free、calloc、realloc动态内存开辟函数
Outlook tutorial, how to use color categories and reminders in outlook?
Redis implements distributed locks
VMware virtual machine network settings
Unity backpack system
Shell:资源监控脚本和高负载报警
超好看的Nteam官网PHP程序源码
每周推荐短视频:如何正确理解“精益”这个词?
关于湖北文理学院平衡信标组的疑问回应
"Xiaodeng" network equipment monitoring in operation and maintenance