当前位置:网站首页>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
边栏推荐
- Home environment monitoring system design (PC version) (mobile app version to be determined)
- OAuth 2.0一键登录那些事
- [batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
- 【批處理DOS-CMD命令-匯總和小結】-外部命令-cmd下載命令、抓包命令(wget)
- ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
- 用太极拳讲分布式理论,真舒服!
- 一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局
- [batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
- Display purchase Summary - Dell 2705qm BenQ pd2700u
- 鸿蒙页面菜单的选择
猜你喜欢

Common functions of OrCAD schematic

realsense d455 semantic_slam实现语义八叉树建图

Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"

LTpowerCAD II和LTpowerPlanner III

栅格地图(occupancy grid map)构建

韩信大招:一致性哈希

Chuantu microelectronics high speed and high performance rs-485/422 transceiver series

VectorDraw Web Library 10.10

VectorDraw Developer Framework 10.10

Sichuan earth microelectronics ca-is1300 isolated operational amplifier for current detection is on the market
随机推荐
Sichuan earth microelectronics ca-is1300 isolated operational amplifier for current detection is on the market
Redis learning notes
高考志愿填报,为啥专业最后考虑?
My debut is finished!
用动图讲解分布式 Raft
【批处理DOS-CMD命令-汇总和小结】-添加注释命令(rem或::)
How comfortable it is to use Taijiquan to talk about distributed theory!
Distributed quorum NWR of the alchemy furnace of the Supreme Master
音频(五)音频特征提取
Tuwei Digital Isolator and interface chip can perfectly replace imported brands Ti and ADI
Tempest HDMI leak receive 2
[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
useMemo模拟useCallback
Static bit rate (CBR) and dynamic bit rate (VBR)
三年营收连续下滑,天地壹号困在醋饮料里
13 `bs_ duixiang. Tag tag ` get a tag object
College entrance examination voluntary filling, why is the major the last consideration?
IAR compiler flashback
Let's talk about MCU crash caused by hardware problems
【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)