skiing
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
5
-
描述
-
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
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
-
输入
-
第一行表示有几组测试数据,输入的第二行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
后面是下一组数据;
输出
- 输出最长区域的长度。 样例输入
-
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
样例输出
-
25
-
第一行表示有几组测试数据,输入的第二行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
#include <stdio.h>
#include <string.h>
int map[100][100];
//记录从i,j下滑的最大值
int res[100][100];
int offset[][2] = {{0,1}, {0,-1}, {1,0}, {-1, 0}};
int row, col, resNumber;
int solve(int r, int c)
{
if(res[r][c])return res[r][c];
int rr, cc, tmp, max=0, hasMin=0;
for(int i=0; i<4; i++)
{
rr = r + offset[i][0];
cc = c + offset[i][1];
if(rr>=0 && rr<row && cc>=0 && cc<col)
{
if(map[rr][cc]<map[r][c])
{
hasMin = 1;
tmp = solve(rr, cc);
if(tmp>max)max=tmp;
}
}
}
++resNumber;
if(hasMin)return res[r][c]=max+1;
return res[r][c]=1;
}
int main()
{
int t, max, mn;
scanf("%d", &t);
while(t--)
{
max = 0, resNumber=0;
memset(res, 0, sizeof(res));
scanf("%d %d", &row, &col);
mn = row*col;
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
scanf("%d", &map[i][j]);
for(int i=0; i<row && resNumber<mn; i++)
for(int j=0; j<col && resNumber<mn; j++)
{
int tmp = solve(i, j);
if(tmp>max)max=tmp;
}
printf("%d\n", max);
}
return 0;
}