当前位置:网站首页>The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
2022-07-03 17:56:00 【REN_ Linsen】
Memory search
Preface
DFS The essence is always violent enumeration , There are often many repeated calculations , As a result, the time complexity soared to the exponential level . And use an array to record the results that have been calculated , Avoid double counting, thereby reducing the waste of time , The essence is space for time , Also known as memory search .
One 、 The number of incremental paths in the grid graph
Two 、 Memory search
package competition.single300;
public class CountPaths {
// You can walk up, down, left and right , In order to replace coordinates with cycles i/j Transformation of , Therefore, the deviation matrix defined in advance .
static final int[][] gaps = new int[][]{
{
-1, 0}, {
1, 0}, {
0, -1}, {
0, 1}};
// In order to make it easy to get the remainder , And the remainder object can be modified uniformly , So use a variable to refer to , Form a primary index .
static final int mod = (int) 1e9 + 7;
// For the convenience of taking the length and width of the lattice , So record the length and width of the lattice with simple variables in advance .
int m, n;
public int countPaths(int[][] grid) {
// Initialize lattice length and width .
m = grid.length;
n = grid[0].length;
// Remember from the... With an array (i,j) The number of starting routes , Not every time dfs To the deepest point .
int[][] f = new int[m][n];
// Converse thinking , Take each grid as the end , With dfs To find the starting point , And record the path length , That is, the number of paths .( No more nodes , Is a new path .)
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) {
// At the end of recursion , No grid is 0, Backtracking without one more grid +1, Represents a new path .( The feeling of reverse thinking , The starting point is the last node found recursively .)
if (i < 0 || j < 0 || i == m || j == n || pre >= grid[i][j]) return 0;
// If you end with this grid , You already know the number of incremental paths , Go straight back , No more dfs We did the calculation .( Space for time )
if (f[i][j] != 0) return f[i][j];
// No passing too This node , There is no backtracking assignment .
f[i][j] = 1;
// dfs Find the number of paths .
for (int[] gap : gaps) {
int ni = i + gap[0], nj = j + gap[1];
// Each sub path comes , Add the current grid , Is a new path .
f[i][j] += dfs(grid, grid[i][j], ni, nj, f);
// Prevent overflow due to excessive accumulation
f[i][j] %= mod;
}
return f[i][j];
}
}
summary
1) Space for time ,DFS One of the typical optimization methods , Memorize . Of course DFS And pruning + Reduce root depth + Reduce the number of roots and other optimization methods .
reference
[1] LeetCode The number of incremental paths in the grid graph
边栏推荐
- link preload prefetch
- AcWing 271. 杨老师的照相排列【多维DP】
- Deops入门
- 1146_ SiCp learning notes_ exponentiation
- [教程]在 CoreOS 上构建你的第一个应用
- Write a program to process a list container of string type. Find a special value in the container 9.27: and delete it if found. Rewrite the above procedure with deque container.
- How to purchase Google colab members in China
- Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
- Vs2013 has blocked the installer, and ie10 needs to be installed
- PR second time
猜你喜欢
As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
(8) HS corner detection
win32:堆破坏的dump文件分析
Deops入门
Three gradient descent methods and code implementation
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
聊聊支付流程的设计与实现逻辑
PHP MySQL inserts multiple pieces of data
Talk about the design and implementation logic of payment process
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
随机推荐
[教程]在 CoreOS 上构建你的第一个应用
Leetcode 669 pruning binary search tree -- recursive method and iterative method
PHP MySQL inserts data
Remote office tools sharing | community essay solicitation
Leetcode Valentine's Day Special - looking for a single dog
Assembly for unloading Loadfrom() loaded assembly - unloading the assembly loaded with assembly LoadFrom()
[mathematical logic] equivalent calculus and reasoning calculus of predicate logic (individual word | predicate | quantifier | predicate logic formula | two basic formulas | proposition symbolization
Vs2013 has blocked the installer, and ie10 needs to be installed
Brief introduction to the core functions of automatic penetration testing tool
聊聊支付流程的設計與實現邏輯
Supervisor monitors gearman tasks
Life perception 1
[combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
解决Zabbix用snmp监控网络流量不准的问题
A. Odd Selection【BruteForce】
Design limitations of structure type (struct)
Redis core technology and practice - learning notes (11): why not just string
聊聊支付流程的设计与实现逻辑
Managing multiple selections with MVVM - managing multiple selections with MVVM