当前位置:网站首页>[dynamic programming] different paths 2
[dynamic programming] different paths 2
2022-04-23 07:17:00 【Breeze_】
link https://leetcode-cn.com/problems/unique-paths-ii
A robot is in a m x n The top left corner of the grid , The robot can only move down or right one step at a time . The robot tries to reach the bottom right corner of the grid , Now consider the obstacles in the grid . So how many different paths will there be from the top left corner to the bottom right corner ?
Definition : d p [ i ] [ j ] dp[i][j] dp[i][j] Indicates a slave index number ( 0 , 0 ) (0,0) (0,0) To index number ( i , j ) (i,j) (i,j) The number of paths taken
The border : First row and first column , d p [ 0 ] [ 0 ] dp[0][0] dp[0][0] Can only be 1. meanwhile , Because you can only go down or right , Therefore, both the first row and the first column can be calculated directly , by 1, If there are obstacles , by 0.
State transition equation : 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
explain : Current value , Equal to the sum of the previous row and the left column , If there are obstacles , by 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]
版权声明
本文为[Breeze_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610323542.html
边栏推荐
- Using stack to realize queue out and in
- Apprentissage par composantes
- Cause: dx. jar is missing
- 扫雷小游戏
- adb shell常用模拟按键keycode
- 谷歌AdMob广告学习
- 利用官方torch版GCN训练并测试cora数据集
- MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
- iTOP4412无法显示开机动画(4.0.3_r1)
- Record WebView shows another empty pit
猜你喜欢
随机推荐
Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
[recommendation of new books in 2021] practical IOT hacking
Component based learning (3) path and group annotations in arouter
Google AdMob advertising learning
电脑关机程序
face_recognition人脸检测
取消远程依赖,用本地依赖
扫雷小游戏
ffmpeg常用命令
BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView 滑动问题
Itop4412 surfaceflinger (4.4.4_r1)
Using queue to realize stack
MySQL笔记1_数据库
PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
PaddleOCR 图片文字提取
ProcessBuilder工具类
WebRTC ICE candidate里面的raddr和rport表示什么?
adb shell top 命令详解
[Exynos4412][iTOP4412][Android-K]添加产品选项
SSL/TLS应用示例