当前位置:网站首页>2328. 网格图中递增路径的数目(记忆化搜索)
2328. 网格图中递增路径的数目(记忆化搜索)
2022-07-06 04:11:00 【Harris-H】
2328. 网格图中递增路径的数目(记忆化搜索)
原理矩阵的四周扩散,也可以记忆化dp。
时间复杂度: O ( n m ) O(nm) O(nm)
class Solution {
public:
int countPaths(vector<vector<int>>& a) {
int n =a.size(),m=a[0].size();
vector<vector<int> >f(n,vector<int>(m,-1));
int d[4][2] ={
0,1,0,-1,1,0,-1,0};
const int mod = 1e9+7;
function<int(int,int)> dfs = [&](int x,int y){
if(~f[x][y]) return f[x][y];
int ans = 1;
for(int i=0;i<4;i++){
int nx = x+d[i][0],ny=y+d[i][1];
if(nx>=0 && nx<n && ny>=0 && ny<m && a[nx][ny] >a[x][y]){
ans=(ans+dfs(nx,ny))%mod;
}
}
return f[x][y] =ans;
};
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++) ans=(ans+dfs(i,j))%mod;
return ans;
}
};
边栏推荐
- Codeforces Round #770 (Div. 2) B. Fortune Telling
- Stable Huawei micro certification, stable Huawei cloud database service practice
- lora网关以太网传输
- TCP/IP协议里面的网关地址和ip地址有什么区别?
- Chinese brand hybrid technology: there is no best technical route, only better products
- Proof of Stirling formula
- [001] [stm32] how to download STM32 original factory data
- Global and Chinese market of aircraft anti icing and rain protection systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Class A, B, C networks and subnet masks in IPv4
- IDEA编译JSP页面生成的class文件路径
猜你喜欢

Ks008 SSM based press release system

【leetcode】1189. Maximum number of "balloons"

Facebook等大廠超十億用戶數據遭泄露,早該關注DID了

查询mysql数据库中各表记录数大小

MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】

1291_Xshell日志中增加时间戳的功能

Basic knowledge of binary tree, BFC, DFS

In Net 6 CS more concise method

Web components series (VII) -- life cycle of custom components

lora网关以太网传输
随机推荐
使用JS完成一个LRU缓存
C form application of C (27)
ESP32_ FreeRTOS_ Arduino_ 1_ Create task
IDEA编译JSP页面生成的class文件路径
Ks008 SSM based press release system
P2648 make money
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
lora网关以太网传输
MySQL master-slave replication
2/13 review Backpack + monotonic queue variant
2/12 didn't learn anything
Path of class file generated by idea compiling JSP page
51nod 1130 n factorial length V2 (Stirling approximation)
Practical development of member management applet 06 introduction to life cycle function and user-defined method
Solution to the problem that the root account of MySQL database cannot be logged in remotely
【leetcode】1189. Maximum number of "balloons"
MySQL about self growth
【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
/usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error: