题目: 传送门
思路: 记忆化搜索,这题不需要vis数组,因为满足要求的路径是高度严格递减的
Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 107;
const int mod = 1000000009;
typedef pair<int, int> P;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int dp[maxn][maxn];
int mp[maxn][maxn];
bool vis[maxn][maxn];
int n, m;
int ans = -1;
int dfs(int sx, int sy) {
if (dp[sx][sy]) return dp[sx][sy];//非零就意味着之前求过了,直接返回
dp[sx][sy] = 1;//因为能走到这一点,所以最小值是1(此处就是重点)
for (int i=0;i<4;i++) {//枚举下一个位置
int nx = sx + dx[i], ny = sy + dy[i];
if (nx>=1&&nx<=n&&ny>=1&&ny<=m) {
if (mp[sx][sy] > mp[nx][ny])//下一个位置高度必须小于现在地高度
dp[sx][sy] = max(dp[sx][sy], dfs(nx, ny) + 1);
}
}
return dp[sx][sy];
}
int main()
{
scanf("%d %d", &n, &m);
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) {
scanf("%d", &mp[i][j]);
}
}
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) {
ans = max(ans, dfs(i, j));
}
}
printf("%d\n", ans);
return 0;
}