• 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
    ", ans);
    63     return 0;
    64 }
  • 相关阅读:
    HDU 4814 Golden Radio Base 模拟
    Java提高篇(三二)-----List总结
    让你提前认识软件开发(28):数据库存储过程中的重要表信息的保存及相关建议
    BBSXP最新漏洞 简单注入检測 万能password
    CodeForce 356A Knight Tournament(set应用)
    POJ--3268--Silver Cow Party【SPFA+邻接表】
    strip 命令的使用方法
    MySQL创建用户权限结果Trigger失败
    Cocos2d-x3.0下一个 Lua与C++打电话给对方
    sharepoint 2013 userprofile 用户信息
  • 原文地址:https://www.cnblogs.com/Arrokoth/p/12191219.html
Copyright © 2020-2023  润新知