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
  • 相关阅读:
    2020年. NET Core面试题
    java Context namespace element 'component-scan' and its parser class ComponentScanBeanDefinitionParser are only available on JDK 1.5 and higher 解决方法
    vue 淡入淡出组件
    java http的get、post、post json参数的方法
    vue 父子组件通讯案例
    Vue 生产环境解决跨域问题
    npm run ERR! code ELIFECYCLE
    Android Studio 生成apk 出现 :error_prone_annotations.jar (com.google.errorprone:error) 错误
    记忆解析者芜青【总集】
    LwIP应用开发笔记之十:LwIP带操作系统基本移植
  • 原文地址:https://www.cnblogs.com/demodemo/p/4732458.html
Copyright © 2011-2022 走看看