zoukankan      html  css  js  c++  java
  • POJ 算法基础 编程题#2: 滑雪

    编程题#2: 滑雪

    来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

    注意: 总时间限制: 1000ms 内存限制: 65536kB

    描述

    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

     1 #include<iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 struct dot {
     5     int row;
     6     int column;
     7     int height;
     8 };
     9 dot matrix[101*101], original[101][101];
    10 int column, row;
    11 int length[101][101];
    12 
    13 bool compare(dot lhs, dot rhs) {
    14     return lhs.height<rhs.height;
    15 }
    16 
    17 int maxStep(int i)
    18 {
    19     int maxStep = 1;
    20     for (int j = 0; j < i; ++j) {
    21         int r = matrix[j].row;
    22         int c = matrix[j].column;
    23         int h = matrix[j].height;
    24         // 按老师的“我为人人”的做法做的
    25         if (r > 1 && original[r-1][c].height > h) {
    26             length[r-1][c] = max(length[r-1][c], length[r][c] + 1);
    27             if (length[r-1][c] > maxStep) {
    28                 maxStep = length[r-1][c];
    29             }
    30         }
    31         if (r < row && original[r+1][c].height > h) {
    32             length[r+1][c] = max(length[r+1][c], length[r][c] + 1);
    33             if (length[r+1][c] > maxStep) {
    34                 maxStep = length[r+1][c];
    35             }
    36         }
    37         if (c > 1 && original[r][c-1].height > h) {
    38             length[r][c-1] = max(length[r][c-1], length[r][c] + 1);
    39             if (length[r][c-1] > maxStep) {
    40                 maxStep = length[r][c-1];
    41             }
    42         }
    43         if (c < column && original[r][c+1].height > h) {
    44             length[r][c+1] = max(length[r][c+1], length[r][c] + 1);
    45             if (length[r][c+1] > maxStep) {
    46                 maxStep = length[r][c+1];
    47             }
    48         }
    49     }
    50     return maxStep;
    51 }
    52 
    53 int main()
    54 {
    55     cin>>row>>column;
    56     int k = 0;
    57     for (int i = 1; i < row+1; ++i) {
    58         for (int j = 1; j < column+1; ++j) {
    59             matrix[k].row = i;
    60             matrix[k].column = j;
    61             cin>>matrix[k].height;
    62             original[i][j] = matrix[k];
    63             length[i][j] = 1; // 所有点的滑行距离先设为1
    64             k++;
    65         }
    66     }
    67     std::sort(matrix, matrix + k, compare);
    68     int maxLength = maxStep(k);
    69     cout<<maxLength;
    70     return 0;
    71 }
  • 相关阅读:
    截取最后一个下划线前面的字符串
    jqgrid加载本地数据功能
    Android、Ios手机端字体根据屏幕分辨率自适应的方法,使用rem和px的区别
    js获取8个月前时间,1天前时间
    手机端/pc端 弹出后,禁止底部页面滚动方法
    列表左右滚动
    jQuery点击隐藏点击显示,计算高度,位置,给当前加上焦点,其他去掉焦点
    工厂模式浅析
    教你看懂UML类图
    Rpc基础篇
  • 原文地址:https://www.cnblogs.com/dagon/p/4849857.html
Copyright © 2011-2022 走看看