[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 9A 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 (starting from and ending at ). Of course, --- --- is longer. In fact, this is the longest one.
Input format
The first line of input represents the row count and column count of a two-dimensional array of regions. Below are the lines, each with numbers representing height (separated by 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 9Sample Output #1
25Prompt
For data of , .
#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; }