NYOJ 10 Skiing 解题报告

来源:互联网 发布:js高德地图轨迹清除 编辑:程序博客网 时间:2024/05/01 03:41

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。
后面是下一组数据;
输出
输出最长区域的长度。
样例输入
15 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
样例输出
25


上图表示一个4*4的滑雪场地图,绿色表格代表边界,为了方便操作,其中存入一个大于所有滑雪场内高度的数据,这里假设boundary = 10500。

灰色表格为滑雪场内的高度分布,每一个格子中,上三角代表该点高度,下三角代表从该点出发的最大路径长度(初始值为0)。

要找出最大路径长度,可以对整个地图中的每个点进行按行从左往右,按列从上向下的顺序遍历。

每个点都要判断它的上,下,左,右四个方向,分别得到从四个方向出发的路径长度,找出其中最大的,加1后存入当前点,即为该点的最大路径长度。

按照上,右,下,左四个方向判断,对于每个方向都要判断高度是否小于当前高度,如果小于的话,判断路径长度是否为0,如果为0则递归到这个方向点,递归返回的是路径长度,如果不为0,则将路径长度记录下来,最后在四个方向中找出最大的路径长度,加1作为该点的最大路径长度。


#include <stdio.h>#define BOUNDARY 10500struct map{int height;int maxlength;}ski_map[105][105];//上,下,左,右四个方向int direction[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};void init_map(int row_num, int column_num){int i;for (i=0; i<row_num+2; i++){ski_map[i][0].height = ski_map[i][column_num + 1].height = BOUNDARY;}for (i=0; i<column_num+2; i++){ski_map[0][i].height = ski_map[row_num + 1][i].height = BOUNDARY;}}int search_maxlength(int x, int y){int i, maxlength, length;ski_map[x][y].maxlength = 1;maxlength = length = 0;for (i=0; i<4; i++){if (ski_map[x + direction[i][0]][y + direction[i][1]].height < ski_map[x][y].height){if (ski_map[x + direction[i][0]][y + direction[i][1]].maxlength == 0){length = search_maxlength(x + direction[i][0], y + direction[i][1]);}else{length = ski_map[x + direction[i][0]][y + direction[i][1]].maxlength;}if (length > maxlength){maxlength = length;}}}return ski_map[x][y].maxlength += maxlength;}int main(void){int i, j, test_num, row_num, column_num, length, maxlength;scanf("%d", &test_num);while (test_num--){scanf("%d%d", &row_num, &column_num);init_map(row_num, column_num);for (i=1; i<=row_num; i++){for (j=1; j<=column_num; j++){scanf("%d", &ski_map[i][j].height);ski_map[i][j].maxlength = 0;}}maxlength = length = 0;for (i=1; i<=row_num; i++){for (j=1; j<=column_num; j++){if (ski_map[i][j].maxlength == 0){length = search_maxlength(i, j);if (length > maxlength){maxlength = length;}}}}printf("%d\n", maxlength);}return 0;}