题意:有一个\(R*C\)的矩阵,可以从矩阵中的任意一个数开始,每次都可以向上下左右选一个比当前位置小的数走,求走到\(1\)的最长路径长度.
题解:这题很明显看到就知道是dfs,但是直接爆搜会TLE,这里我们采用记忆化搜索,什么意思呢?
? 我们在dfs的时候,每次都记录一下每个点到1的路径长度,然后,之后当我选到这个位置的时候, 我们就可以直接选用这个点的路径长度,也就没有必要再去对这个点进行dfs了.这样大大节省了时间.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
using namespace std;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
int r,c;
int a[10000][10000];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int res[10000][10000];
int dfs(int x,int y){
if(res[x][y]) return res[x][y];
res[x][y]=1;
for(int i=0;i<4;++i){
int tx=x+dx[i];
int ty=y+dy[i];
if((tx>=1&&tx<=r)&&(ty>=1&&ty<=c)&&(a[x][y]>a[tx][ty])){
dfs(tx,ty);
res[x][y]=max(res[x][y],res[tx][ty]+1);
}
}
return res[x][y];
}
int main() {
ios::sync_with_stdio(false);
cin>>r>>c;
for(int i=1;i<=r;++i){
for(int j=1;j<=c;++j){
cin>>a[i][j];
}
}
int ans=0;
for(int i=1;i<=r;++i){
for(int j=1;j<=c;++j){
ans=max(ans,dfs(i,j));
}
}
printf("%d\n",ans);
return 0;
}
原文:https://www.cnblogs.com/lr599909928/p/12891221.html