当前位置:网站首页>【動態規劃】不同路徑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
边栏推荐
- Exploration of SendMessage principle of advanced handler
- 组件化学习(1)思想及实现方式
- JVM basics you should know
- Android清除应用缓存
- MySQL notes 5_ Operation data
- 常用UI控件简写名
- Binder机制原理
- [sm8150] [pixel4] LCD driver
- Encapsulate a set of project network request framework from 0
- [2021 book recommendation] kubernetes in production best practices
猜你喜欢

【2021年新书推荐】Artificial Intelligence for IoT Cookbook

Google AdMob advertising learning

C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported

BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题

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

Record WebView shows another empty pit

Cause: dx.jar is missing

Itop4412 LCD backlight drive (PWM)

Cancel remote dependency and use local dependency

c语言编写一个猜数字游戏编写
随机推荐
个人博客网站搭建
利用官方torch版GCN训练并测试cora数据集
Component learning
adb shell top 命令详解
.net加载字体时遇到 Failed to decode downloaded font:
Tiny4412 HDMI display
Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
PyTorch 模型剪枝实例教程三、多参数与全局剪枝
Binder mechanism principle
树莓派:双色LED灯实验
Fill the network gap
记录webView显示空白的又一坑
图像分类白盒对抗攻击技术总结
iTOP4412无法显示开机动画(4.0.3_r1)
AVD Pixel_2_API_24 is already running.If that is not the case, delete the files at C:\Users\admi
MySQL5.7插入中文数据,报错:`Incorrect string value: ‘\xB8\xDF\xAE\xF9\x80 at row 1`
ThreadLocal,看我就够了!
Itop4412 cannot display boot animation (4.0.3_r1)
PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
【2021年新书推荐】Professional Azure SQL Managed Database Administration