跳到正文

个人记录

P1434 [SHOI2002] Skiing

Lost_Boy约 5 分钟
原文
文章信息与版本

文章信息

收录
2026年5月5日
更新
2026年5月5日
篇幅
约 5 分钟

版本

  1. v1当前

[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−124-17-16-1 (starting from 2424 and ending at 11). Of course, 2525 -2424 -2323 - …\dots-33 -22 -11 is longer. In fact, this is the longest one.

Input format

The first line of input represents the row count RR and column count CC of a two-dimensional array of regions. Below are the RR lines, each with CC numbers representing height (separated by 11 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%100\%, 1≤R,C≤1001 \leq R, C \leq 100 .

C++
#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; }
原文