ljx_gkx @ 2023-03-29 01:13:01
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 310;
int h[N][N]; //山高;
int f[N][N];
int dx[4] = {-1, 1, 0, 0}; // 方向数组
int dy[4] = {0, 0, -1, 1};
int n, m;
int dfs (int x, int y)
{
if (f[x][y] != -1) return f[x][y];
f[x][y] = 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 && h[x][y]>h[a][b])
f[x][y] = max(f[x][y], dfs(a, b) + 1);
}
return f[x][y];
}
int main()
{
cin >> n >> m;
for (int i=1; i <= n; i++)
for (int j=1; j <= m; j ++)
cin >> h[i][j];
memset (f, -1, sizeof(f));
int res=0;
for (int i=1; i <= n; i ++)
for (int j=1; j <= m; j ++)
res = max(res, dfs(i, j));
cout << res;
return 0;
}
by LEZ2012 @ 2023-04-22 17:05:03
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int n,m,a[201][201],s[201][201],ans;
bool use[201][201];
int dfs(int x,int y){
if(s[x][y])return s[x][y];
s[x][y]=1;
for(int i=0;i<4;i++)
{ int xx=dx[i]+x;
int yy=dy[i]+y;
if(xx>0&&yy>0&&xx<=n&&yy<=m&&a[x][y]>a[xx][yy]){
dfs(xx,yy);
s[x][y]=max(s[x][y],s[xx][yy]+1);
}
}
return s[x][y];
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
ans=max(ans,dfs(i,j));
printf("%d",ans);
return 0;
}
AC代码(求关注)