当前位置: 首页 > 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.gsyq.cn/news/1444647.html

相关文章:

  • FortiGate 7.4.2 新机开箱第一步:从接上网线到设置中文界面的保姆级避坑指南
  • 如何快速掌握Illustrator脚本:提升设计效率的完整实战指南
  • WSL2 Ubuntu 20.04 装完Docker报错?别慌,一个命令切换iptables模式就能搞定
  • 2026年5月无溶剂环氧涂料工厂推荐,环氧酚醛/光固化保护套/石墨烯涂料/无溶剂环氧涂料,无溶剂环氧涂料批发厂家怎么选 - 品牌推荐师
  • 2026年管道式电磁流量计TOP5选型参考名录:管道式电磁流量计、蒸汽涡街流量计、超声波液位计、一体化温度变送器选择指南 - 优质品牌商家
  • 网络编程的三要素
  • 用micro:bit与舵机制作交互式纸板机器人:从电容触摸到机械传动
  • 告别过曝死黑!用Python+OpenCV玩转HDR多曝光融合,手机拍的照片也能救回来
  • 在Python中TCP网络程序开发的步骤流程
  • Sora 2社交媒体视频实战手册(含TikTok/小红书/Instagram三端首发合规清单)
  • 避坑指南:CellChat v2空间细胞通讯分析中,这些参数设置和可视化细节千万别忽略
  • 2026佛山H型钢专业采购技术指南:佛山钢板加工、佛山钢结构、佛山镀锌钢材、佛山镀锌钢管、珠三角钢材市场、佛山圆钢选择指南 - 优质品牌商家
  • 别再乱用TCP_NODELAY了!用Wireshark抓包实测Nagle算法对Java Socket性能的真实影响
  • 2026年边坡防护网厂家选型推荐 核心维度实测对比 - 优质品牌商家
  • Sora 2水印不是“贴图”而是动态神经水印——2024年OpenAI最新专利解读及对抗性去除路径(附TensorRT加速部署)
  • 实测对比:同步整流Buck芯片 vs 老古董LM2596,效率、发热和体积差了多少?
  • Veo 2人物一致性失效的7个致命盲区:从ID Embedding断裂到姿态时序漂移的工业级修复手册
  • 2026年西安老酒回收实体门店出价与服务排行盘点:西安老五粮液回收、西安老茅台回收、西安老西凤酒回收、西安茅台酒回收选择指南 - 优质品牌商家
  • 告别WebUI!ComfyUI最新便携版Windows保姆级安装教程(含模型共享与汉化)
  • 2026双边丝围栏网技术解析:选型、工艺与厂家实测对比 - 优质品牌商家
  • Simulink里调用Adams整车模型:从机械导出到控制闭环的完整配置流程
  • 2026年知名的大型蹦床/温州室内蹦床定制加工厂家推荐 - 行业平台推荐
  • 2026年6月,衡水房屋设计市场如何选择?这五家信誉与实力兼备的公司值得深入了解 - 2026年企业资讯
  • Windows 11上OpenVINO 2023.2保姆级安装教程:从Python 3.8到Demo测试,一次搞定所有依赖
  • 实时动作仿真精度提升4.8倍?Sora 2动捕模拟的3层隐式约束机制首次公开
  • 从单细胞到空间定位:如何用GEO数据(GSE138794)和CARD重构肿瘤微环境细胞图谱
  • 探索BetterRTX安装器:为Minecraft Bedrock版开启光线追踪新纪元
  • 合法酒店物资回收怎么结算,服客再生资源费用低吗 - myqiye
  • 别再手动下载了!用FTP+脚本自动化备份海量ADS-B历史数据(Linux/Windows教程)
  • 在Ubuntu 20.04上,用musl工具链为ARM板子交叉编译libffi(附踩坑记录)