poj_1088也可以说的上是经典的DP问题了。
DP的核心思想是审时度势。过去的最优+当下的最优,有点迭代的意思。所以很多题都可以考虑递归。只是递归太耗资源,不一定是最好的选择,所以要考虑改进。这道题可以作为一个改进的参考。
//--------------------------------------------------------------------------------------------------------------------------------------------
using namespace std;
int max(int a,int b,int c,int d)
{
int t1 = a>b?a:b;
int t2 = c>d?c:d;
return t1>t2?t1:t2;
}
int re[101][101];
int h[101][101];
int R,C;
int DP(int i,int j)
{
int t1=0,t2=0,t3=0,t4=0;
if(re[i][j] > 0)
return re[i][j];
re[i][j] = 1;
if(i > 1)
{
if(h[i][j] > h[i-1][j])
{
t1 = DP(i-1,j);
}
}
if(i < R)
{
if(h[i][j] > h[i+1][j])
{
t2 = DP(i+1,j);
}
}
if(j > 1)
{
if(h[i][j-1] < h[i][j])
{
t3 = DP(i,j-1);
}
}
if(j < C)
{
if(h[i][j+1] < h[i][j])
{
t4 = DP(i,j+1);
}
}
re[i][j] = max(t1,t2,t3,t4)+1;
return re[i][j];
}
int main()
{
int i,j;
cin>>R>>C;
for(i = 1; i <= 101 ;i++)
for(j = 1;j <= 101;j++)
re[i][j] = 0;
for(i = 1; i <= R ;i++)
for(j = 1;j <= C;j++)
cin>>h[i][j];
int t1,t2,t3,t4;
int MAX;
for(i = 1; i <= R ;i++)
for(j = 1;j <= C;j++)
{
DP(i,j);
}
MAX = -1;
for(i = 1;i <= R ;i++)
for(j = 1;j <= C;j++)
{
if(re[i][j] > MAX)
MAX = re[i][j];
}
cout<<MAX<<endl;
return 0;
}