当前位置:网站首页>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
边栏推荐
猜你喜欢

Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)

Debian introduction

Evolution of Alibaba e-commerce architecture

TEMPEST HDMI泄漏接收 1

FairMOT yolov5s转onnx

Kube scheduler source code analysis (1) - initialization and startup analysis

VectorDraw Web Library 10.10

PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering

Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection

Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver
随机推荐
ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
VectorDraw Developer Framework 10.10
Application scheme | application of Sichuan earth microelectronics ca-is398x in PLC field
【批处理DOS-CMD命令-汇总和小结】-外部命令-cmd下载命令、抓包命令(wget)
数据可视化没有重点怎么办?
Unity3D邪门实现之GUI下拉菜单Dropdown设计无重复项
[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
LabVIEW jump to web page
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令(cd、title、mode、color、pause、chcp、exit)
Accès à la boîte aux lettres du nom de domaine Lead à l'étranger
高考志愿填报,为啥专业最后考虑?
Collection of common terms and meanings in forestry investigation based on lidar
【蒸馏】PointDistiller: Structured Knowledge DistillationTowards Efficient and Compact 3D Detection
el-input实现尾部加字
[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
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
Chuantuwei ca-is3720lw alternative material No. iso7820fdw
[QT] shortcut key
[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
Cocos learning diary 3 - API acquisition nodes and components