poj 1088滑雪

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

重做了一遍这到n年前做过的题,原先的代码是这样的
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 111
 4 #define MAX(x,y) x>y?x:y
 5 int huaxue[111][111];                                                    
 6 int len[111][111];
 7 
 8 int getmax(int i,int j,int R,int C,int huaxue[][111],int len[][111])
 9 {
10       int temp=1;
11       
12         if (i-1>=0&&huaxue[i-1][j]<huaxue[i][j])
13          temp=1+(len[i][j]==0?getmax(i-1,j,R,C,huaxue,len):len[i-1][j]);
14         if (i+1<=R-1&&huaxue[i+1][j]<huaxue[i][j])
15          temp=(MAX(temp,1+(len[i][j]==0?getmax(i+1,j,R,C,huaxue,len):len[i+1][j])  ) );
16         if (j-1>=0&&huaxue[i][j-1]<huaxue[i][j])
17          temp=(MAX(temp,1+(len[i][j]==0?getmax(i,j-1,R,C,huaxue,len):len[i][j-1])  ) );
18         if (j+1<=C-1&&huaxue[i][j+1]<huaxue[i][j])
19          temp=(MAX(temp,1+(len[i][j]==0?getmax(i,j+1,R,C,huaxue,len):len[i][j+1])  ) ) ;
20         
21         len[i][j]=temp;
22     
23     return temp;
24 }
25 main()
26 {
27     int R,C,i,j,result;
28     //freopen("input.txt","r",stdin);
29     scanf("%d %d",&R,&C);
30     
31     for(i=0;i<R;i++)
32     {
33         for(j=0;j<C;j++)
34         scanf("%d",&huaxue[i][j]);
35         len[i][j]=0;
36     }
37      result=0;
38     
39     for(i=0;i<R;i++)
40       for(j=0;j<C;j++)
41         
42             result=(MAX(result,getmax(i,j,R,C,huaxue,len)));
43         
44     
45     printf("%d\n",result);
46     return 0;
47 }

这次提交的代码是这样的

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 
 5 int height[102][102];
 6 int cnt[102][102];
 7 int dir[][4] = {{0,1},{0,-1},{1,0},{-1,0}};
 8 int r,c;
 9 
10 int dfs(int x, int y) {
11     if(cnt[x][y] != 0) {
12         return cnt[x][y];
13     }
14     int tmpMax = 1;
15     for(int i = 0; i < 4; i++) {
16         int tmpx = x + dir[i][0];
17         int tmpy = y + dir[i][1];
18         int tmpC = 0;
19         if(tmpx >= 0 && tmpy >= 0 && tmpx < r && tmpy < c && height[tmpx][tmpy] < height[x][y]) {
20             tmpC = 1 + dfs(tmpx, tmpy);
21         }
22         if(tmpMax < tmpC) {
23             tmpMax = tmpC;
24         }
25     }
26     cnt[x][y] = tmpMax;
27     return tmpMax;
28 }
29 
30 int main(int argc, char const *argv[])
31 {
32     //freopen("input.txt","r",stdin);
33     while(scanf("%d %d",&r,&c) != EOF) {
34         memset(cnt,0,sizeof(cnt));
35         for(int i = 0; i < r; i++) {
36             for(int j = 0; j < c; j++) {
37                 scanf("%d",&height[i][j]);
38             }
39         }
40         int max = 1;
41         for(int i = 0; i < r; i++) {
42             for(int j = 0; j < c; j++) {
43                 int tmp = dfs(i,j);
44                 if(max < tmp) {
45                     max = tmp;
46                 }
47             }
48         }
49         printf("%d\n",max);
50     }
51     return 0;
52 }

第一次提交居然没通过,因为第18行tmpC没有初始化!

posted @ 2016-08-14 16:19  Jason杰  阅读(182)  评论(0编辑  收藏  举报