洛谷—— P1434 滑雪

https://www.luogu.org/problem/show?pid=1434#sub

题目描述

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(从24开始,在1结束)。当然25-24-23―┅―3―2―1更长。事实上,这是最长的一条。

输入输出格式

输入格式:

 

输入的第一行为表示区域的二维数组的行数R和列数C(1≤R,C≤100)。下面是R行,每行有C个数,代表高度(两个数字之间用1个空格间隔)。

 

输出格式:

 

输出区域中最长滑坡的长度。

 

输入输出样例

输入样例#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
输出样例#1:
25

记忆化搜索
 1 #include <algorithm>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 const int N(233);
 7 int n,m,ans;
 8 int len[N][N],map[N][N];
 9 
10 int fx[4]={0,1,-1,0};
11 int fy[4]={1,0,0,-1};
12 int DFS(int x,int y)
13 {
14     if(len[x][y]) return len[x][y];
15     int ret=1;
16     for(int i=0;i<4;i++)
17     {
18         int xx=x+fx[i],yy=y+fy[i];
19         if(xx<1||yy<1||xx>n||yy>m) continue;
20         if(map[x][y]<=map[xx][yy]) continue;
21         int temp=DFS(xx,yy)+1;
22         ret=max(ret,temp);
23     }
24     len[x][y]=ret;
25     return ret;
26 }
27 
28 int main()
29 {
30     scanf("%d%d",&n,&m);
31     for(int i=1;i<=n;i++)
32       for(int j=1;j<=m;j++)
33         scanf("%d",&map[i][j]);
34     for(int i=1;i<=n;i++)
35       for(int j=1;j<=m;j++)
36           ans=max(ans,len[i][j]=DFS(i,j));
37     printf("%d",ans); 
38     return 0;
39 }

 

posted @ 2017-07-09 10:24  Aptal丶  阅读(271)  评论(0编辑  收藏  举报