zoukankan      html  css  js  c++  java
  • 算法练习——滑雪

    • 题目描述
        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更长。事实上,这是最长的一条。
    • 输入格式描述
        输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。例子:
       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
    • 输出格式
       25
    解题思路:
    动态规划+贪婪算法 递归算法
    求一个点的最大长度,先求相邻点的最大长度
    /**
    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
    */
    
    #include<stdio.h>
    #include <algorithm>
    
    #define N 100
    
    int map[N][N],len[N][N]; //map存放点高度信息,len存放对于点可以滑的最大长度
    int n=0,m=0; //n表示图行数,m表示图的列数
    int MAX=0; //整个图的最大长度
    
    //获得 map[a,b] 点的最大长度,顺带算出目前整个图中最大长度
    int search(int a,int b);
    // 动态规划+贪心算法
    int main(){    
        int i,j;
        //map
        while(~scanf("%d %d",&n,&m)){        
            for(i=0;i<n;i++)
                for(j=0;j<m;j++){
                    scanf("%d",&map[i][j]);                
                }                
            memset(len,0,sizeof(len)); //初始化各个点的最大长度
            for(i=0;i<n;i++)
                for(j=0;j<m;j++){
                    search(i,j);                
                }
            printf("%d
    ",MAX);
        }
        return 0;
    }
    
    int search(int a,int b){
        if(len[a][b] != 0) return len[a][b];
        int max = 0;
        //
        if(a-1>=0&&map[a][b] > map[a-1][b]){
            if(max < search(a-1,b)) max = search(a-1,b);
        }
        //
        if(b-1>=0&&map[a][b] > map[a][b-1]){
            if(max < search(a,b-1)) max = search(a,b-1);
        }
        //
        if(b+1<m&&map[a][b] > map[a][b+1]){
            if(max < search(a,b+1)) max = search(a,b+1);
        }
        //
        if(a+1<n&&map[a][b] > map[a+1][b]){
            if(max < search(a+1,b)) max = search(a+1,b);
        }
        len[a][b] = max+1;    
        if(MAX < len[a][b])
            MAX = len[a][b];
        return len[a][b];
    }
    View Code
  • 相关阅读:
    中国历史朝代公元对照简表
    [Solved] DashBoard – Excel Service: The data sources may be unreachable, may not be responding, or may have denied you access.
    Delete/Remove Project from TFS 2010
    Sharepoint site showing system account instead of my username on the top right corner.
    你的成功在于你每天养成的习惯
    Internet Information Services is running in 32bit emulation mode. Correct the issue listed above and rerun setup.
    Prepare to back up and restore a farm (Office SharePoint Server 2007)
    Word中字号与磅值的对应关系
    How to: Change the Frequency for Refreshing the Data Warehouse for Team System
    UI Automation in WPF/Silverlight
  • 原文地址:https://www.cnblogs.com/zhuqingqin/p/5408127.html
Copyright © 2011-2022 走看看