POJ 1088 滑雪 (深搜+DP)

时间:2020-01-15
本文章向大家介绍POJ 1088 滑雪 (深搜+DP),主要包括POJ 1088 滑雪 (深搜+DP)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

http://poj.org/problem?id=1088

题目描述:

Glory非常喜欢玩滑滑梯游戏,下面给出了一个n,m的滑道,其中的数字表示滑道的高度。Glory可以从一个点出发向下滑行,每次只能滑行到相邻的位置(上下左右)中高度严格低于当前高度的地方,不能重复划行已经滑行过的地方,但他希望在这个滑道上滑行尽量远的距离,也即是找一条最长的滑道。

Input

第一行输入两个数n,m代表滑梯范围行n和列m(1 <= n,m <= 100)。下面是n行,每行有m个整数,代表高度h,(0<=h<=20000)

Output

输出一个值,代表Glory能够在滑滑梯上面滑行的最长长度是多少

Sample Input

3 3
9 1 2
5 6 7
8 4 3

Sample Output

4

Sample Input

4 7
7 6 5 4 3 2 1
1 5 1 1 1 1 1
1 4 3 1 1 1 1
1 5 6 7 8 1 1

Sample Output

7

hint

样例1:7->6->4->3 长度为4

面向题解的思路:

我觉得有点类似于求连通区域个数的问题,因为都是在一块地图上走嘛。

记录的目的是为了防止超时。

数组dp中存放的就是每个点所能够走的最远距离。

过程主要是记录某个点能够到达的最长的长度。然后由某一个点向四个方向延伸,到达某点的时候,如果这点已经遍历过的话,就直接返回值就行了,否则就由这一点去遍历。

代码:

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <climits>
 6 #include <map>
 7 #include <set>
 8 #include <queue>
 9 #include <stack>
10 #include <vector>
11 #include <string>
12 #include <iostream>
13 #include <algorithm>
14 
15 #define N 100010
16 
17 using namespace std;
18 
19 typedef long long int ll;
20 int n, m;
21 int rec[110][110];
22 int grap[110][110];
23 int dx[4]={0, 1, 0, -1}; int dy[4]={1, 0, -1, 0};
24 
25 bool torf(int i, int j){
26     return (i>=0 && i<n && j>=0 && j<m);    //判断是否越界
27 }
28 //找距离此点的最远距离
29 int find_max(int i, int j){
30     if(rec[i][j]>0)         //如果已经遍历此点
31         return rec[i][j];
32     int k, maxl;
33     int nx, ny;
34     maxl=0;
35     //四个方向的遍历
36     for(k=0; k<4; k++){
37         nx=i+dx[k];
38         ny=j+dy[k];
39         //充要的条件还有下一点的价值大于目前点的价值
40         if(torf(nx, ny) && grap[nx][ny]>grap[i][j]){
41             maxl=max(maxl, find_max(nx, ny));
42         }
43     }
44     return rec[i][j]=maxl+1;    //能够走的最远距离加上本身
45 }
46 int main()
47 {
48     int i, j;
49     scanf("%d%d", &n, &m);
50     for(i=0; i<n; i++){
51         for(j=0; j<m; j++){
52             scanf("%d", &grap[i][j]);
53             rec[i][j]=0;
54         }
55     }
56     int ans=INT_MIN;
57     for(i=0; i<n; i++){
58         for(j=0; j<m; j++){
59             ans=max(ans, find_max(i, j));   //每次搜索就更新一下值
60         }
61     }
62     printf("%d\n", ans);
63     return 0;
64 }

原文地址:https://www.cnblogs.com/Arrokoth/p/12191219.html