OpenJudge 1088 Skiing

Total time limit: 1000ms Memory Limit: 65536kB

description

Michael likes to ski hundred This is not surprising, because skiing is very exciting. However, in order to gain speed, slippery areas must be downward, and when you slide into the base, you have to up the grade again or wait for the elevator to take you. Michael wants to know is contained in a region the longest landslide. Region is given by a two-dimensional array. Each number represents the height of the point of the array. Below is an example

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

A person can slide up and down from a point adjacent to one of the four points, if and only if the height is reduced. In the above example, a landslide is coasting 24-17-16-1. Of course 25-24-23 -...- 3-2-1 longer. In fact, this is the longest one.

Entry

The first line represents the input line number of the R zone and the number of columns C (1 <= R, C <= 100). The following is R rows, each row having a C integer representing the height h, 0 <= h <= 10000. The longest length of the output region.

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

Problem-solving ideas

A "man for me" type, calculated from low to high, the maximum MaxPath MaxPath each point is lower than its left and right upper and lower points + 1.

How to implement sorting, sorting the points stored in a new array, review the use of the sort function.

Because not familiar with the sort function, reference is made to someone else's code, regarded as a lesson, bone up this question.

AC Code

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int map[105][105];//存储高度
int maxPath[105][105];
int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };

struct Node
{
    int x;
    intY;
     int H;
     BOOL  operator <( const the Node & F) const 
    { 
        return H < FH; 
    } 
} n-[ 10100 ]; 

int main () {
     int R & lt, C;
     int CNT = 0 ; // n-[] of angle standard 
    int ANS = . 1 ; 
    CIN >> R & lt >> C;
     for ( int I = . 1 ; I <= R & lt; I ++) // zeroth row and zeroth column of air out of the corner where uniform 
    {
         for ( int= J . 1 ; J <= C; J ++ ) 
        { 
            CIN >> Map [I] [J]; 
            MaxPath [I] [J] = . 1 ; // initializes. 1 
            n-[CNT] .x = I; 
            n-[CNT ] .y = J; 
            [CNT] n-.h = Map [I] [J]; // read the data node, to prepare to sort 
            CNT ++ ; 
        } 
    } 
    Sort (n-, n- + CNT);
     for ( int I = 0 ; I <CNT; I ++) // from low to high through all points 
    {
         int X =n-[I] .x;
         int Y = n-[I] .y;
         for ( int J = 0 ; J < . 4 ; J ++) // vertical and horizontal four directions 
        {
             int XX = X + the dir [J] [ 0 ] ;
             int YY = Y + the dir [J] [ . 1 ];
             IF (XX> = . 1 && XX <= R & lt && YY> = . 1 && YY <= C && Map [X] [Y]> Map [XX] [YY ]) // every elapse of a point (x, y), to update their 
            { 
                MaxPath [X] [Y] = max (MaxPath [X] [Y], MaxPath [XX] [YY] + . 1 ); 
                ANS= max(ans, maxPath[x][y]);
            }
        }
    }
    cout << ans << endl;
    //system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/yun-an/p/10964069.html