zoukankan      html  css  js  c++  java
  • 滑雪

    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

    题意:
      求从map上的一点开始滑行 ,滑行的最大距离

    很简单的滑雪问题,step【i】[j]表示从该点能 划的最远距离 他等于max(step[k][l])  map[k][l]<step[i][j];
    这个题目用记忆化搜索还是比较方便的
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    #define MAX 100
    
    int map[MAX][MAX];
    int temp[MAX][MAX];
    int r,c;
    
    void Init(){
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                cin>>map[i][j];
            }
        }
        memset(temp,0,sizeof(temp));
    }
    
    int Step(int x,int y)
    {
        if(temp[x][y]>0)
            return temp[x][y];
        int max = 0;
        if(x>0)
            if( map[x][y]>map[x-1][y] )
                if( max < Step(x-1,y) )
                    max = Step(x-1,y);
        if(x<r-1)
            if( map[x][y]>map[x+1][y] )
                if( max < Step(x+1,y) )
                    max = Step(x+1,y);
        if(y>0)
            if( map[x][y]>map[x][y-1] )
                if( max < Step(x,y-1) )
                    max = Step(x,y-1);
        if(y<c-1)
            if( map[x][y]>map[x][y+1] )
                if( max < Step(x,y+1) )
                    max = Step(x,y+1);
        temp[x][y] = max + 1;
    
        return temp[x][y];
    }
    
    int Search()
    {
        int max=0;
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                Step(i,j);
                if( max < temp[i][j] )
                    max = temp[i][j];
            }
        }
        return max;
    }
    
    int main()
    {
        while(cin>>r>>c)
        {
            Init();
            cout<<Search()<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    IOS快速集成下拉上拉刷新
    无限互联IOS电影项目视频笔记
    Unix时间戳(Unix timestamp)转换工具
    ***php(codeigniter)中如何重定向
    CodeIgniter 错误: In order to use the Session class you are required to set an encryption key
    一个Form中2个按钮,PHP后台如何判断提交的是哪一个按钮
    PHP 判断是否包含某字符串
    Linux那些事儿之我是Hub(大结局)挂起自动化【转】
    Linux USB Host-Controller的初始化代码框架分析【转】
    urb传输的代码分析【转】
  • 原文地址:https://www.cnblogs.com/demodemo/p/4732458.html
Copyright © 2011-2022 走看看