poj 1088 滑雪

滑雪
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 89171   Accepted: 33477

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

Source

 
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #define N 100+10
 5 
 6 using namespace std;
 7 
 8 const int dx[5]={1,-1,0,0};     //四种可能的走的方向 
 9 const int dy[5]={0,0,1,-1};
10 int r,c;
11 int f[N][N];
12 int h[N][N];
13 
14 void init();                   //读入 
15 int search(int,int);           //查找 
16 void doit();
17 
18 int main()
19   {
20       init();
21       doit();
22       return 0;
23   }
24 void init()
25   {
26       scanf("%d%d",&r,&c);
27       for(int i=1;i<=r;i++)
28         for(int j=1;j<=c;j++)
29           scanf("%d",&h[i][j]);
30   }
31 
32 void doit()
33   {
34       int ans=0;
35       for(int i=1;i<=r;i++)
36         for(int j=1;j<=c;j++)           //遍历从每一个点出发可能走到的点的数量,然后取大就是最大长度 
37           {
38               f[i][j]=search(i,j);
39               ans=f[i][j]>ans?f[i][j]:ans;
40           }
41     printf("%d
",ans);
42   }
43 
44 int search(int x,int y)
45   {
46       if(f[x][y]) return f[x][y];        //记忆性搜索,节省时间 
47       int t=1;
48       for(int i=0;i<4;i++)               //四种走法 
49         {
50             int nx=x+dx[i];
51             int ny=y+dy[i];
52             if(h[x][y]<h[nx][ny])          //判断能不能划过去 
53               {
54                   t=max(t,search(nx,ny)+1);  //看向那边划能划得最远,取最优情况 
55               }
56         }
57     f[x][y]=t;                          //记忆性搜索 
58     return t;
59   }
滑雪
原文地址:https://www.cnblogs.com/yuemo/p/5495290.html