POJ_1088,二维数组动规题目,结合记忆化搜索(体现在length数组上,保存每个子问题结果)
#include <iostream>
#include <cstring>
using namespace std;
int height[101][101];
int length[101][101];
int r,c;
int get_length(int i,int j){
if(i<0 || i>=r || j<0 || j>=c) return -1; //越界的长度为-1,这样比较值为0不会有影响
if(length[i][j]!=0) return length[i][j]; //已经得到结果的子问题直接返回结果
int temp=0;
if(height[i][j]>height[i-1][j]) //四个方向上若比当前点高度低则递归
temp=max(temp,get_length(i-1,j)+1);
if(height[i][j]>height[i+1][j])
temp=max(temp,get_length(i+1,j)+1);
if(height[i][j]>height[i][j-1])
temp=max(temp,get_length(i,j-1)+1);
if(height[i][j]>height[i][j+1])
temp=max(temp,get_length(i,j+1)+1);
return temp;
}
int main(){
int ans=0;
cin>>r>>c;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>height[i][j];
memset(length,0,sizeof(length));
for(int i=0;i<r;i++)
for(int j=0;j<c;j++){
length[i][j]=get_length(i,j); //将每个子问题的结果保存在数组length中
ans=max(ans,length[i][j]);
}
cout<<ans+1; //开始点没算长度这里补上
return 0;
}