当前位置:网站首页>网格图中递增路径的数目[dfs逆向路径+记忆dfs]
网格图中递增路径的数目[dfs逆向路径+记忆dfs]
2022-07-03 17:54:00 【REN_林森】
前言
DFS本质是一直暴力枚举法,往往存在很多的重复计算,从而导致时间复杂度飙升至指数级别。而用数组记录曾经计算过结果,防止重复计算从而减少时间的浪费,本质就是空间换时间,也称记忆化搜索。
一、网格图中递增路径的数目

二、记忆化搜索
package competition.single300;
public class CountPaths {
// 上下左右都可以走,为了用循环代替坐标i/j的变换,所以采用提前定义好的偏差矩阵。
static final int[][] gaps = new int[][]{
{
-1, 0}, {
1, 0}, {
0, -1}, {
0, 1}};
// 为了方便取余时写的简单,且可统一修改取余对象,所以用一个变量指代,形成一级索引。
static final int mod = (int) 1e9 + 7;
// 为了方便取格子的长宽,所以提前用简单变量记录格子长宽。
int m, n;
public int countPaths(int[][] grid) {
// 初始化格子长宽。
m = grid.length;
n = grid[0].length;
// 用数组记住从第(i,j)出发的路径个数,不用每次都dfs到最深处。
int[][] f = new int[m][n];
// 逆向思维,以每个格子作为终点,以dfs的方式来寻找起点,并记录路径长度,即路径个数。(没多一个节点,就是一个新路径。)
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
ans += dfs(grid, 1 << 31, i, j, f);
ans %= mod;
}
}
return ans;
}
private int dfs(int[][] grid, int pre, int i, int j, int[][] f) {
// 递归结束处,无格子就是0,回溯没多一个格子就+1,表示一条新路径。(优点逆向思维的感觉,起点在递归寻找到的最后一个节点。)
if (i < 0 || j < 0 || i == m || j == n || pre >= grid[i][j]) return 0;
// 如果以该格子为终点,已经知道递增路径的个数了,就直接返回,不用再dfs进行计算了。(空间换时间)
if (f[i][j] != 0) return f[i][j];
// 没有路过 过 这个节点,就没有回溯给其赋值。
f[i][j] = 1;
// dfs寻找路径数。
for (int[] gap : gaps) {
int ni = i + gap[0], nj = j + gap[1];
// 每条分路径过来,加上当前格子,都是一个新路径。
f[i][j] += dfs(grid, grid[i][j], ni, nj, f);
// 防止累计过多而溢出
f[i][j] %= mod;
}
return f[i][j];
}
}
总结
1)空间换时间,DFS典型的优化方式之一,记忆化。当然DFS还有剪枝+减少根深度+减少根个数等等优化方式。
参考文献
边栏推荐
- PHP MySQL create database
- A. Berland Poker &1000【简单数学思维】
- Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
- Five problems of database operation in commodity supermarket system
- PHP returns 500 errors but no error log - PHP return 500 error but no error log
- Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
- 1164 Good in C
- PHP MySQL Update
- QT adjust win screen brightness and sound size
- WebView module manages the application window interface to realize the logical control and management operation of multiple windows (Part 1)
猜你喜欢

Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up

Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028

Introduction to SolidWorks gear design software tool geartrax

MySQL has been stopped in the configuration interface during installation

SQL injection database operation foundation

互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码

Interviewer: why is the value nil not equal to nil?
![[combinatorics] generating function (summation property)](/img/74/e6ef8ee69ed07d62df9f213c015f2c.jpg)
[combinatorics] generating function (summation property)

互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼

win32:堆破坏的dump文件分析
随机推荐
PHP MySQL preprocessing statement
毕业总结
Graduation summary
小程序 多tab 多swiper + 每个tab分页
Baiwen.com 7 days Internet of things smart home learning experience punch in the next day
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
Getting started with deops
PHP MySQL order by keyword
A. Berland Poker &1000【简单数学思维】
Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
聊聊支付流程的设计与实现逻辑
Redis core technology and practice - learning notes (VII) sentinel mechanism
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
国内如何购买Google Colab会员
Kotlin的协程:上下文
解决Zabbix用snmp监控网络流量不准的问题
Analyse ArrayList 3: suppression d'éléments
Talk about the design and implementation logic of payment process
QT learning diary 9 - dialog box
PR second time