P1434 [SHOI2002] Skiing

· · 个人记录

[SHOI2002] Skiing

Title Description

Michael likes skiing. This is not surprising, as skiing is indeed very exciting. But in order to gain speed, the sliding area must tilt downwards, and when you reach the bottom of the slope, you have to go up the slope again or wait for the elevator to pick you up. Michael wants to know the longest landslide in an area. The region is given by a two-dimensional array. Each number in the array represents the height of the point. Here 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 from a certain point to one of the four adjacent points, up, down, left, and right, if and only if the height decreases. In the above example, a feasible landslide is 24-17-16-1 (starting from 24 and ending at 1). Of course, 25 -24 -23 - \dots -3 -2 -1 is longer. In fact, this is the longest one.

Input format

The first line of input represents the row count R and column count C of a two-dimensional array of regions. Below are the R lines, each with C numbers representing height (separated by 1 space between two numbers).

Output format

The length of the longest landslide in the output area.

Example #1

Sample Input #1

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 #1

25

Prompt

For data of 100\%, 1 \leq R, C \leq 100 .

#include <bits/stdc++.h>
#define A 4
#define B 2
#define C 1
#define D 0
#define E -1
#define F 105
using namespace std;
int direction[A][B] = {C, D, D, C, D, E, E, D };
int _map[F][F], r, c, answer, DP[F][F];
int DFS(int x, int y) {
    if (DP[x][y]) {
        return DP[x][y]; }
    int _max = C;
    for (int i = D; i < A; i++) {
        int new_x = x + direction[i][D];
        int new_y = y + direction[i][C];
        if (D < new_x && new_x <= r && D < new_y && new_y <= c && _map[new_x][new_y] < _map[x][y]) {
            _max = max(_max, DFS(new_x, new_y) + C); } }
    DP[x][y] = _max;
    return DP[x][y]; }
int main(void) {
    cin >> r >> c;
    for (int i = C; i <= r; i++) {
        for (int j = C; j <= c; j++) {
            cin >> _map[i][j]; } }
    for (int i = C; i <= r; i++) {
        for (int j = C; j <= c; j++) {
            answer = max(answer, DFS(i, j)); } }
    cout << answer;
    return D; }