• POJ 1088 滑雪(记忆化搜索)


    滑雪

    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<stdio.h>
     2 # include<string.h>
     3 # include<queue>
     4 using namespace std;
     5 int ans,r,c;
     6 int dx[] = {1,0,0,-1};
     7 int dy[] = {0,1,-1,0};
     8 struct Node{
     9     int height;
    10     bool vis;    //该结点是否被访问,记忆化搜索的根据
    11     int length;    //从该结点出发能够达到的最大长度
    12 };
    13 
    14 Node node[105][105];
    15 
    16 int f(int x,int y){    //搜索从该结点出发经历的最大长度
    17     if(node[x][y].vis)
    18         return node[x][y].length;
    19     node[x][y].vis = true;
    20     int i,xx,yy,max=1;
    21     for(i=0;i<4;i++){    //向四个方向
    22         xx = x+dx[i];
    23         yy = y+dy[i];
    24         if(xx<1 || yy<1 ||xx>r || yy>c) continue;
    25         if(node[xx][yy].height >= node[x][y].height) continue;
    26         int temp = 1 + f(xx,yy);
    27         if(temp>max) max = temp;
    28     }
    29     node[x][y].length = max;
    30     return max;
    31 }
    32 int main(){
    33 //    freopen("in.txt","r",stdin);
    34     int i,j;
    35     while(scanf("%d%d",&r,&c)!=EOF){
    36         for(i=1;i<=r;i++)
    37             for(j=1;j<=c;j++){
    38                 scanf("%d",&node[i][j].height);
    39                 node[i][j].vis = false;
    40                 node[i][j].length = 0;
    41             }
    42             ans = 0;
    43             for(i=1;i<=r;i++)
    44                 for(j=1;j<=c;j++){
    45                     int temp = f(i,j);
    46                     ans = ans>temp ?ans : temp;
    47                 }
    48                 printf("%d
    ",ans);                    
    49     }
    50     return 0;
    51 }
     
  • 相关阅读:
    Nova创建虚拟机的底层代码分析
    Chapter 1 Securing Your Server and Network(12):保护链接server
    c语言实现hashtable,相似C++的map和iOS的NSDictionary
    Android_Training
    Bean Validation 技术规范特性概述
    linux下串口调试工具/串口终端推荐: picocom
    Android:主题(Theme)
    oracle database 12c R1 安装文档
    python版本wifi共享工具
    iOS 6编程Cookbook(影印版)
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3281789.html
Copyright © 2020-2023  润新知