序号:1980
[SHOI2002]滑雪
Time Limit:1s Memory Limit:128MB
题目描述
Michael 喜欢滑雪。这并不奇怪,因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael 想知道在一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子:
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
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度会减小。在上面的例子中,一条可行的滑坡为 2424-1717-1616-11(从 2424 开始,在 11 结束)。当然 2525-2424-2323-……-33-22-11 更长。事实上,这是最长的一条。
输入格式
输入的第一行为表示区域的二维数组的行数 RR 和列数 CC。下面是 RR 行,每行有 CC 个数,代表高度(两个数字之间用 11 个空格间隔)。
输出格式
输出区域中最长滑坡的长度。
输入输出样例
输入 #1
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
输出 #1
25
说明/提示
对于 100%100% 的数据,1≤R,C≤1001≤R,C≤100。
#include <iostream>
#include<cstdio>
#include <time.h>
#include <cstring>
int h[100][100]={0};
int count[100][100]={0};
int f[][2]={{1,0},{0,1},{-1,0},{0,-1}};
int r,c;
int max_line = 0;
int find(int i,int j)
{
int max_t=0;
if(count[i][j]>0)
return count[i][j];
//up search
if(i-1>=0)
{
if(h[i-1][j]<h[i][j])
{
max_t = std::max(find(i-1,j),max_t);
}
}
//down search
if(i+1<r)
{
if(h[i+1][j]<h[i][j])
{
max_t = std::max(find(i+1,j),max_t);
}
}
//left search
if(j-1>=0)
{
if(h[i][j-1]<h[i][j])
max_t = std::max(find(i,j-1),max_t);
}
//right search
if(j+1<c)
{
if(h[i][j+1]<h[i][j])
{
max_t = std::max(find(i,j+1),max_t);
}
}
count[i][j]=max_t+1;
return max_t+1;
}
int main() {
memset( count , -1 , sizeof ( count ) );
scanf("%d%d", &r, &c);
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
{
scanf("%d", &h[i][j]);
}
int ans = 0;
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
{
count[i][j]=find(i,j);
ans = std::max(ans, count[i][j]);
}
printf("%d", ans);
return 0;
}