滑雪
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 88484 | Accepted: 33177 |
Description
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更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
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 分析:看到这个题感觉好简单,直接递归就写好了,提交居然TLE了。那么就需要一些小技巧了,先干掉动态分配二维数组,然后定义一个二维数组gawDis[]用来记录对应节点的最长路径,后面再用到这个节点的时候就不需要再计算了。综上:递归+记忆化dfs搜索
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define MAXNUM 101 5 6 int gawMap[MAXNUM][MAXNUM]; 7 int gawRow; 8 int gawColumn; 9 int gawDis[MAXNUM][MAXNUM]; 10 11 int max(int a, int b) 12 { 13 if(a>b) 14 return a; 15 else 16 return b; 17 } 18 19 int Ski(int wlasthigh, int *awlastposition) 20 { 21 int awposition[2] = {0}; 22 int wleft = 1; 23 int wright = 1; 24 int wup = 1; 25 int wdown = 1; 26 27 if(gawDis[awlastposition[0]][awlastposition[1]] != 0) 28 { 29 return gawDis[awlastposition[0]][awlastposition[1]]; 30 } 31 if(awlastposition[1] > 0) 32 { 33 awposition[1] = awlastposition[1]-1; 34 awposition[0] = awlastposition[0]; 35 if(wlasthigh > gawMap[awposition[0]][awposition[1]]) 36 { 37 wleft = Ski(gawMap[awposition[0]][awposition[1]], awposition) + 1; 38 } 39 } 40 if(awlastposition[1] < gawColumn-1) 41 { 42 awposition[1] = awlastposition[1]+1; 43 awposition[0] = awlastposition[0]; 44 if(wlasthigh > gawMap[awposition[0]][awposition[1]]) 45 { 46 wright = Ski(gawMap[awposition[0]][awposition[1]], awposition) + 1; 47 } 48 } 49 if(awlastposition[0] > 0) 50 { 51 awposition[0] = awlastposition[0]-1; 52 awposition[1] = awlastposition[1]; 53 if(wlasthigh > gawMap[awposition[0]][awposition[1]]) 54 { 55 wup = Ski(gawMap[awposition[0]][awposition[1]], awposition) + 1; 56 } 57 } 58 if(awlastposition[0] < gawRow-1) 59 { 60 awposition[0] = awlastposition[0]+1; 61 awposition[1] = awlastposition[1]; 62 if(wlasthigh > gawMap[awposition[0]][awposition[1]]) 63 { 64 wdown = Ski(gawMap[awposition[0]][awposition[1]], awposition) + 1; 65 } 66 } 67 wleft = max(wleft, wright); 68 wup = max(wup, wdown); 69 return gawDis[awlastposition[0]][awlastposition[1]] = max(wleft, wup); 70 } 71 72 int main(void) 73 { 74 int i = 0; 75 int j = 0; 76 scanf("%d %d", &gawRow, &gawColumn); 77 78 for(i=0; i<gawRow; i++) 79 { 80 for(j=0; j<gawColumn; j++) 81 { 82 scanf("%d", &gawMap[i][j]); 83 } 84 } 85 86 int awposition[2] = {0}; 87 for(i=0; i<gawRow; i++) 88 { 89 for(j=0; j<gawColumn; j++) 90 { 91 awposition[1] = j; 92 awposition[0] = i; 93 Ski(gawMap[awposition[0]][awposition[1]], awposition); 94 } 95 } 96 97 int wLen = 0; 98 for(i=0; i<gawRow; i++) 99 { 100 for(j=0; j<gawColumn; j++) 101 { 102 if(wLen < gawDis[i][j]) wLen = gawDis[i][j]; 103 } 104 } 105 printf("%d\n", wLen); 106 107 return 0; 108 }
时间: 2024-10-03 21:41:12