#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int len[105][105];
int height[105][105];
int max(int a,int b,int c,int d)
{
int e = a > b ? a : b;
int f = c > d ? c : d;
return e > f ? e : f;
}
int dfs(int i,int j,int h)
{
if(height[i][j]==-1||h<=height[i][j]) return 0;//corner
if(len[i][j]>=0) return len[i][j];//already visited
//dp
len[i][j]=1+max(dfs(i-1,j,height[i][j]),dfs(i+1,j,height[i][j]),dfs(i,j-1,height[i][j]),dfs(i,j+1,height[i][j]));
return len[i][j];
}
int main()
{
//freopen("poj1088.txt","r",stdin);
int r,c;
while(cin>>r>>c)
{
memset(len,-1,sizeof(len));
memset(height,-1,sizeof(height));
int i,j;
for(i=1;i!=r+1;i++)
for(j=1;j!=c+1;j++)
{
cin>>height[i][j];
}
int maxlen=-1;
for(i=1;i!=r+1;i++)
for(j=1;j!=c+1;j++)
{
int tmplen=dfs(i,j,height[i][j]+1);//height[i][j]+1 ensure first search begining
if(tmplen>maxlen)
maxlen=tmplen;
}
cout<<maxlen<<endl;
}
return 0;
}
北大POJ1088 dp入门
最新推荐文章于 2018-10-01 15:55:06 发布