当前位置:网站首页>【 planification dynamique】 différentes voies 2
【 planification dynamique】 différentes voies 2
2022-04-23 07:16:00 【Petit vent】
Liens https://leetcode-cn.com/problems/unique-paths-ii
Un robot dans un m x n Coin supérieur gauche de la grille,Le robot ne peut se déplacer qu'un pas vers le bas ou vers la droite à la fois.Le robot essaie d'atteindre le coin inférieur droit de la grille,Considérez maintenant les obstacles dans la grille.Combien de chemins différents vont suivre du coin supérieur gauche au coin inférieur droit?
Définition: d p [ i ] [ j ] dp[i][j] dp[i][j]Indique le numéro d'index ( 0 , 0 ) (0,0) (0,0)Au numéro d'index ( i , j ) (i,j) (i,j)Le nombre de trajets
Frontière:Première ligne et première colonne, d p [ 0 ] [ 0 ] dp[0][0] dp[0][0]Ça ne peut être que ça.1.En même temps,Comme vous ne pouvez marcher qu'en bas ou à droite,La première ligne et la première colonne sont donc directement calculables,Pour1,Si vous rencontrez un obstacle,Pour0.
Équation de transfert d'état: 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
Description:Valeur actuelle, égal à la somme de la ligne précédente et de la colonne de gauche ,Si vous rencontrez un obstacle,Pour0
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]
版权声明
本文为[Petit vent]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610323542.html
边栏推荐
猜你喜欢
Ffmpeg common commands
Android面试计网面经大全【持续更新中。。。】
[recommendation of new books in 2021] practical IOT hacking
Fill the network gap
Android清除应用缓存
微信小程序 使用wxml2canvas插件生成图片部分问题记录
iTOP4412 HDMI显示(4.0.3_r1)
机器学习笔记 一:学习思路
C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported
Itop4412 HDMI display (4.4.4_r1)
随机推荐
记录webView显示空白的又一坑
【2021年新书推荐】Learn WinUI 3.0
【2021年新书推荐】Practical IoT Hacking
Personal blog website construction
Itop4412 LCD backlight drive (PWM)
Cause: dx. jar is missing
给女朋友写个微信双开小工具
Android room database quick start
机器学习 二:基于鸢尾花(iris)数据集的逻辑回归分类
Miscellaneous learning
MySQL notes 1_ database
图像分类白盒对抗攻击技术总结
从0开始封装一套项目的网络请求框架
.net加载字体时遇到 Failed to decode downloaded font:
Reading notes - activity
Component learning
Using queue to realize stack
Itop4412 HDMI display (4.4.4_r1)
iTOP4412 HDMI显示(4.4.4_r1)
三种实现ImageView以自身中心为原点旋转的方法