当前位置:网站首页>【動態規劃】不同路徑2
【動態規劃】不同路徑2
2022-04-23 07:16:00 【小風_】
鏈接 https://leetcode-cn.com/problems/unique-paths-ii
一個機器人比特於一個 m x n 網格的左上角,機器人每次只能向下或者向右移動一步。機器人試圖達到網格的右下角,現在考慮網格中有障礙物。那麼從左上角到右下角將會有多少條不同的路徑?
定義: d p [ i ] [ j ] dp[i][j] dp[i][j]錶示從索引號 ( 0 , 0 ) (0,0) (0,0)到索引號 ( i , j ) (i,j) (i,j)的路徑走法的數量
邊界:第一行和第一列, d p [ 0 ] [ 0 ] dp[0][0] dp[0][0]只能是1。同時,由於只能下走或右走,因此第一行和第一列都是可直接計算的,為1,如果遇到障礙,為0。
狀態轉移方程: d p [ i ] [ j ] = { d p [ i − 1 ] [ j ] + d p [ i ] [ j − 1 ] , o b s t a c l e G r i d [ i ] [ j ] = 0 0 , o b s t a c l e G r i d [ i ] [ j ] ≠ 0 d p[i][j]= \begin{cases}d p[i-1][j]+dp[i][j-1], & obstacleGrid[i][j]~=\operatorname 0 \\ 0, & obstacleGrid[i][j]~≠\operatorname 0\end{cases} dp[i][j]={ dp[i−1][j]+dp[i][j−1],0,obstacleGrid[i][j] =0obstacleGrid[i][j] =0
說明:當前值,等於上一行和左一列的和,如果遇到障礙,為0
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid):
m = len(obstacleGrid)
n = len(obstacleGrid[0])
dp = [[0 for i in range(n)] for j in range(m)]
for i in range(n):
if obstacleGrid[0][i]==1:
break
dp[0][i] = 1
for i in range(m):
if obstacleGrid[i][0]==1:
break
dp[i][0] = 1
for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] == 0:
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
return dp[m-1][n-1]
版权声明
本文为[小風_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610323542.html
边栏推荐
- 杂七杂八的学习
- 【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
- 机器学习 二:基于鸢尾花(iris)数据集的逻辑回归分类
- AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
- 机器学习笔记 一:学习思路
- 微信小程序 使用wxml2canvas插件生成图片部分问题记录
- ThreadLocal,看我就够了!
- Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
- MySQL笔记3_约束_主键约束
- Apprentissage par composantes
猜你喜欢

取消远程依赖,用本地依赖
![Android interview Online Economic encyclopedia [constantly updating...]](/img/48/dd1abec83ec0db7d68812f5fa9dcfc.png)
Android interview Online Economic encyclopedia [constantly updating...]

【2021年新书推荐】Learn WinUI 3.0

图像分类白盒对抗攻击技术总结

Cause: dx. jar is missing

组件化学习(1)思想及实现方式

ArcGIS License Server Administrator 无法启动解决方法

组件化学习(2)Arouter原理学习

Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation

微信小程序 使用wxml2canvas插件生成图片部分问题记录
随机推荐
Cause: dx. jar is missing
组件化学习(3)ARouter中的Path和Group注解
[recommendation for new books in 2021] professional azure SQL managed database administration
补补网络缺口
个人博客网站搭建
Itop4412 HDMI display (4.0.3_r1)
图像分类白盒对抗攻击技术总结
三子棋小游戏
深度学习模型压缩与加速技术(一):参数剪枝
Thanos.sh灭霸脚本,轻松随机删除系统一半的文件
Component based learning (3) path and group annotations in arouter
winform滚动条美化
Record WebView shows another empty pit
[recommendation of new books in 2021] enterprise application development with C 9 and NET 5
利用队列实现栈
Handler进阶之sendMessage原理探索
ProcessBuilder工具类
iTOP4412 HDMI显示(4.4.4_r1)
[Andorid] 通过JNI实现kernel与app进行spi通讯
MySQL笔记2_数据表