Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 56656 | Accepted: 20556 |
Description
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
Output
Sample Input
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Sample Output
25
经过很长时间的努力终于做出来了。这是一个典型的备忘录问题,将求出来的最长距离用一个二维数组保存起来。。。减少工作量。。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int height[102][102];
int s[102][102];
int r,c;
int get_height(int i,int j)
{
if(s[i][j]>0)return s[i][j];
int max_length=0;
//up
if(i-1>0&&height[i][j]>height[i-1][j])
{
int x=get_height(i-1,j)+1;
if(max_length<x)
{
max_length=x;
}
//max_length=max(max_length,x);
}
//down
if(i+1<=r&&height[i][j]>height[i+1][j])
{
int x=get_height(i+1,j)+1;
if(max_length<x)
{
max_length=x;
}
//max_length=max(max_length,x);
}
//left
if(j-1>0&&height[i][j]>height[i][j-1])
{
int x=get_height(i,j-1)+1;
if(max_length<x)
{
max_length=x;
}
//max_length=max(max_length,x);
}
//right
if(j+1<=c&&height[i][j]>height[i][j+1])
{
int x=get_height(i,j+1)+1;
if(max_length<x)
{
max_length=x;
}
//max_length=max(max_length,x);
}
return max_length;
}
int main()
{
//cin>>r>>c;
scanf("%d%d",&r,&c);
for(int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
//cin>>height[i][j];
scanf("%d",&height[i][j]);
s[i][j]=0;
}
}
int _max=0;
for(int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
s[i][j]=get_height(i,j);//注意保存
_max=max(_max,s[i][j]);
}
}
cout<<_max+1<<endl;
return 0;
}