POJ 1088 滑雪(模板题 DFS+记忆化)

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

题解:这道题目的思路还是挺好找的,不知道终点,不知道起点,直接遍历所有点开始搜索,记得要加一个记忆化,不然会超时,
  发现自己有点问题,写代码的时候思路不清晰,有点依赖于模板,这个要改正,没事的时候可以多看看代码,码代码能力实在是不太行。
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int maxn=105;
 8 int m,n;
 9 int Map[maxn][maxn],dp[maxn][maxn];
10 
11 int DFS(int x, int y)
12 {
13     if(dp[x][y]!=-1)
14         return dp[x][y];
15 
16     int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
17     int tmax=0;
18     for(int i=0; i<4; i++)
19     {
20         int nx=dx[i]+x, ny=dy[i]+y;
21         if(nx>=0&&nx<m&&ny>=0&&ny<n &&Map[nx][ny] < Map[x][y])
22             tmax=max(tmax, DFS(nx, ny));
23     }
24     dp[x][y]=tmax+1;
25 
26     return tmax+1;
27 }
28 
29 int main ()
30 {
31     //freopen("in.txt", "r", stdin);
32     memset(Map, -1, sizeof(Map));
33     memset(dp, -1, sizeof(dp));
34 
35     cin>>m>>n;
36     for(int i=0; i<m; i++)
37         for(int j=0; j<n; j++)
38             cin>>Map[i][j];
39 
40     int ans=0;
41     for(int i=0; i<m; i++)
42         for(int j=0; j<n; j++)
43         {
44             dp[i][j]=DFS(i,j);
45             ans=max(dp[i][j], ans);
46         }
47 
48     cout<<ans<<endl;
49     return 0;
50 }


原文地址:https://www.cnblogs.com/Yokel062/p/10606000.html