dfs+dp一下,因为序列下降保证了 dfs不会 tle ,对于 每一个点 ,直接扫即可 ,同时更新最大值;
AC代码
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int a[110][110];
int cnt[110][110];
int dir[4][2]={1,0,0,1,0,-1,-1,0};
int m,n;
int dp(int x,int y)
{
if (cnt[x][y])
return cnt[x][y];
for (int i=0;i<4;i++)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if (xx>=1&&xx<=n&&yy>=1&&yy<=m)
{
if (a[x][y]>a[xx][yy])
{
if (cnt[x][y]<dp(xx,yy)+1)
cnt[x][y]=cnt[xx][yy]+1;
}
}
}
return cnt[x][y];
}
int main()
{
while (cin>>n>>m)
{
int maxn=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
cin>>a[i][j];
memset(cnt,0,sizeof(cnt));
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
maxn=max(maxn,dp(i,j));
cout<<maxn+1<<endl;
}
return 0;
}