Files
python/TangDou/AcWing/JYH/901.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 310;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int g[N][N]; // 邻接矩阵,地图
int f[N][N]; // 记录从i到j的最大距离
int n, m; // 行与列
int res; // 结果
// 记忆化搜索
int dfs(int x, int y) {
if (~f[x][y]) return f[x][y];
// 求最长的轨迹,最起码是1
f[x][y] = 1;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
if (g[x][y] > g[tx][ty])
f[x][y] = max(f[x][y], dfs(tx, ty) + 1);
}
return f[x][y];
}
int main() {
cin >> n >> m; // n行 m列
// 读入每一个点的高度
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
// 初始化-1
memset(f, -1, sizeof f);
// 从每一个点出发
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
res = max(res, dfs(i, j));
// 输出结果
printf("%d\n", res);
return 0;
}