当前位置: 首页 > news >正文

Unique Paths II(动态规划)

Unique Paths II

更多技术博客 http://vilins.top/

题目

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Example 1:

Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:

  1. Right -> Right -> Down -> Down
  2. Down -> Down -> Right -> Right

分析

这里是典型的动态规划问题,通过将大问题分解为小问题来不断求解。
我们这里用本来的二维数组作为存储结果的数组,那我们分析一下这样使用的可用性,在对原始obstacleGrid[i][j]使用之前,我们只会使用前面的obstacleGrid[i-1][j]和obstacleGrid[i][j-1]的数据,所以我们这样使用不会改变初始数据,并且可以节省空间。
其次,我们来看看状态转移方程,我们用的二维数组表示的时,当前这个点之前可以选择的所有路径,由于路径的选择只有向下和向右这两个方向,如果当前格子有障碍物的话,到当前格子的路径数为0,那么我们得出以下状态转移方程:

if(obstacleGrid[i][j] == 0) { obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1]; } else { obstacleGrid[i][j] = 0; }

源码

class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int row = obstacleGrid.size(); int col = obstacleGrid[0].size(); if(row == 0||col == 0) { return 0; } if(obstacleGrid[0][0] == 1) { return 0; } obstacleGrid[0][0] = 1; for(int i = 1; i < row; i++) { if(obstacleGrid[i][0] == 0) { obstacleGrid[i][0] = obstacleGrid[i-1][0]; } else { obstacleGrid[i][0] = 0; } } for(int i = 1; i < col; i++) { if(obstacleGrid[0][i] == 0) { obstacleGrid[0][i] = obstacleGrid[0][i-1]; } else { obstacleGrid[0][i] = 0; } } for(int i = 1; i < row; i++) { for(int j = 1; j < col; j++) { if(obstacleGrid[i][j] == 0) { obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1]; } else { obstacleGrid[i][j] = 0; } } } return obstacleGrid[row-1][col-1]; } };

更多技术博客 http://vilins.top/

http://www.jsqmd.com/news/933305/

相关文章:

  • 格式规范否?8款AI论文写作工具梯队榜,毕业答辩稳了!
  • 【Sora 2倒放视频生成黑科技】:全球仅3家实验室验证的时序逆向建模方法首度公开
  • 2026年6月,北京花洒置物平台服务商深度解析:为何恒洁卫浴成为品质之选? - 2026年企业资讯
  • 统计思维实战自测:提升数据决策力,避开常见认知陷阱
  • AI生成图能注册版权吗?(美国版权局2023-2024全部裁定原文深度拆解)
  • 保姆级教程:用Python和Pandas快速上手UJIIndoorLoc室内定位数据集
  • 2026年管道式电磁流量计TOP5选型参考名录:管道式电磁流量计、蒸汽涡街流量计、超声波液位计、一体化温度变送器选择指南 - 优质品牌商家
  • FreeSWITCH新手避坑指南:第一次用fs_cli必须知道的3个关键点和1个危险操作
  • 网络编程的三要素
  • 惊了!输入题目,这几款AI写作辅助软件就能生成图文并茂的毕业论文
  • 用micro:bit与舵机制作交互式纸板机器人:从电容触摸到机械传动
  • OV系列摄像头SCCB总线配置避坑指南:从三线到两线,时序参数怎么调才稳定?
  • 告别VCP!用FTDI D2XX库直接驱动MPSSE引擎(以FT2232H为例,含C++/Qt代码)
  • 别再只跑默认参数了!TransDecoder 5.7.1高级参数调优与结果深度解读指南
  • 电玩城游戏机实测评测:电玩城游戏机、文审游戏机、出票游戏机、商用游戏机、实物五门文审机、扣篮王游戏机、扣篮王选择指南 - 优质品牌商家
  • Arduino JCB挖掘机模型:从机电一体化到3D打印的完整实践指南
  • Edit Distance(动态规划)
  • 告别过曝死黑!用Python+OpenCV玩转HDR多曝光融合,手机拍的照片也能救回来
  • 在Python中TCP网络程序开发的步骤流程
  • 别再只会apt-get install了!遇到pkgProblemResolver依赖错误,试试这个更聪明的aptitude命令
  • Sora 2社交媒体视频实战手册(含TikTok/小红书/Instagram三端首发合规清单)
  • 避坑指南:CellChat v2空间细胞通讯分析中,这些参数设置和可视化细节千万别忽略
  • RT-Thread在RA4M2上跑飞了?手把手教你用Cortex-M33的Fault寄存器定位Hardfault(附排查流程图)
  • AI商业应用实战:从单点工具到全链条重构的落地指南
  • 别再乱用TCP_NODELAY了!用Wireshark抓包实测Nagle算法对Java Socket性能的真实影响
  • 告别虚拟机!在Win10上为GAMMA搭建MSYS2+WinPython轻量级开发环境实录
  • 上海原配追讨财产律师权威排行:上海老公给小三转的钱怎么要回、上海虹口婚外情维权律师、上海起诉小三流程和费用、上海起诉小三返还财产律师选择指南 - 优质品牌商家
  • 2026佛山H型钢专业采购技术指南:佛山钢板加工、佛山钢结构、佛山镀锌钢材、佛山镀锌钢管、珠三角钢材市场、佛山圆钢选择指南 - 优质品牌商家
  • 从SQL Server的CHARINDEX到C#的IndexOf:一次搞懂跨层字符串查找的‘索引差’问题
  • 算法设计与分析--动态规划(十)