$$\color{red}{算法}\color{blue}{基础课}\color{purple}{笔记and题解}\color{green}{汇总}$$
考虑爆搜求出最优解。然后发现会超时,所以对每一个点记忆化它为起点的最优解,就可以直接过了。
#include <bits/stdc++.h>
using namespace std;
const int N = 315;
int n, m, f[N][N], h[N][N], ans;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int dfs(int x, int y) {
int &v = f[x][y];
if (v != -1) return v;
v = 1;
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx > 0 && xx <= n && yy > 0 && yy <= m && h[x][y] > h[xx][yy]) v = max(v, dfs(xx, yy) + 1);
}
return v;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf("%d", &h[i][j]), f[i][j] = -1;
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;
}