思路:
代码:
#include<iostream>
#include<cstring>
using namespace std;
const int N = 305;
int n, m;
int g[N][N], f[N][N];
int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
int dp(int x,int y){
if(f[x][y] != -1) return f[x][y];
int &v = f[x][y];
v = 1;
for(int i = 0; i < 4; i++){
int a = x + dx[i], b = y + dy[i];
if(a >= 1 && a <= n && b >= 1 && b <= m && g[a][b] < g[x][y]){
v = max(v, dp(a, b) + 1);
}
}
return f[x][y];
}
int main(){
cin >> n >> m;
memset(f, -1, sizeof f);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> g[i][j];
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
ans = max(ans, dp(i , j));
}
}
cout << ans;
return 0;
}
评论区