You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

2 years ago
#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;
}