当前位置:网站首页>【 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
边栏推荐
猜你喜欢
随机推荐
JVM basics you should know
launcher隐藏不需要显示的app icon
补补网络缺口
org. xml. sax. SAXParseException; lineNumber: 141; columnNumber: 252; cvc-complex-type. 2.4. a: Found element 'B
Itop4412 surfaceflinger (4.4.4_r1)
[2021 book recommendation] kubernetes in production best practices
利用栈实现队列的出队入队
Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide
机器学习笔记 一:学习思路
xcode 编译速度慢的解决办法
webView因证书问题显示一片空白
AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
[recommendation for new books in 2021] professional azure SQL managed database administration
.net加载字体时遇到 Failed to decode downloaded font:
MySQL笔记1_数据库
C#新大陆物联网云平台的连接(简易理解版)
Tiny4412 HDMI display
Bottomsheetdialogfragment conflicts with listview recyclerview Scrollview sliding
[2021 book recommendation] artistic intelligence for IOT Cookbook








