当前位置:网站首页>Different paths ii[dynamic planning improvement for DFS]
Different paths ii[dynamic planning improvement for DFS]
2022-06-25 07:40:00 【REN_ Linsen】
Preface
Dynamic programming , Common space for time , Especially for DFS for , Reduce a lot of unnecessary double counting .
One 、 Different paths II
Two 、DP + In situ space utilization
package everyday.medium;
// Different paths II
public class UniquePathsWithObstacles {
/* target: Go from the upper left corner of the matrix to the lower right corner , Possible ways , You can't walk where there are obstacles . The first idea is DFS+ Over obstacles , however DFS There are many repeated sub paths , So you can exchange space for time , With dynamic programming . Each grid comes from the left or the top , So you can calculate how many ways to walk to the lower right corner step by step . // notes : Tips , In situ modification matrix , Use it , Reduce space complexity . */
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
// because 1 Indicates an obstacle ,0 Indicates accessibility , So when modifying the matrix in place , Use a negative number to indicate the possible number of paths .
int m = obstacleGrid.length, n = obstacleGrid[0].length;
// A special case , If both the starting point and the ending point are obstacles , You don't have to go .
if (obstacleGrid[0][0] == 1 || 1 == obstacleGrid[m - 1][n - 1]) return 0;
obstacleGrid[0][0] = -1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// If it's an obstacle , Then it is not necessary to calculate .
if (obstacleGrid[i][j] == 1) continue;
int up = i == 0 || obstacleGrid[i - 1][j] == 1 ? 0 : obstacleGrid[i - 1][j];
int left = j == 0 || obstacleGrid[i][j - 1] == 1 ? 0 : obstacleGrid[i][j - 1];
// The current grid is the end point , The number of possible paths is Coming from the left + On the right .
// bug1: When i == j == 0 when , The initialization of the -1 Also covered , All for 0
// obstacleGrid[i][j] = left + up;
obstacleGrid[i][j] += left + up;
}
}
// Return to the bottom right corner , Number of possible paths .
return -obstacleGrid[m - 1][n - 1];
}
}
summary
1) Dynamic programming .
reference
边栏推荐
- VectorDraw Web Library 10.10
- 无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
- VectorDraw Web Library 10.10
- shell小技巧(一百三十四)简单的键盘输入记录器
- useMemo模拟useCallback
- 稳压二极管的原理,它有什么作用?
- LabVIEW generate application (exe) and installer
- Common functions of OrCAD schematic
- 【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
- Evolution of Alibaba e-commerce architecture
猜你喜欢
CPDA|数据分析师成长之路如何起步?
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
不同路径II[针对DFS的动态规划改进]
我的处女作杀青啦!
【LeetCode】two num·两数之和
太上老君的炼丹炉之分布式 Quorum NWR
为什么要“除夕”,原来是内存爆了!
Cocos learning diary 3 - API acquisition nodes and components
Zhugeliang vs pangtong, taking distributed Paxos
[Introduction aux uvm== > Episode 9] ~ modèle de registre, intégration du modèle de registre, méthode conventionnelle du modèle de registre, scénario d'application du modèle de registre
随机推荐
keepalived監控進程,自動重啟服務進程
Mysql database import SQL file display garbled code
Why "New Year's Eve", the original memory burst!
Global variables & local variables
Ca-is1200u current detection isolation amplifier has been delivered in batch
13 `bs_duixiang.tag标签`得到一个tag对象
What is the difference between norflash and nandflash
Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)
VectorDraw Web Library 10.10
Construction of occupancy grid map
shell小技巧(一百三十四)简单的键盘输入记录器
海思3559 sample解析:vio
China Mobile MCU product information
MySQL face Scripture eight part essay
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
JMeter introduction practice ----- use of global variables and local variables
Orcad Schematic常用功能
【Qt】快捷键
【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks