Skiing
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:77763 |
|
Accepted:28905 |
Description
It's not surprising that Michael loves skiing because skiing is really exciting. But in order to get the speed, the slide area must be tilted down, and when you slide to the bottom, you have to go uphill again or wait for the elevator to carry you. Michael wants to know the longest landslide in a region. A region is given by a two-dimensional array. Each number in the array represents the vertex height. The following is an example.
1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
A person can slide from a certain point to one of the four adjacent points up and down, when and only when the height is reduced. In the preceding example, a slide is 24-17-16-1. Of course, 25-24-23-...-3-2-1 is longer. In fact, this is the longest one.
Input
The first line indicates the number of rows in the region R and the number of columns C (1 <= r, C <= 100 ). Below is the R row, each row has a C integer, representing the height H, 0 <= H <= 10000.
Output
The length of the maximum output area.
Sample Input
5 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
Sample output
25
Source
Shtsc 2002
Idea:
When you search for a State at a Coordinate Position (this state is the optimal state), you can also find the optimal state of other coordinates. In this way, when you search for the optimal state of the next coordinate, return when the optimal state of a location has been obtained, that is, the searched result does not need to be searched again.
For example, in this question, step [I] [J] defines how many steps (not including yourself) can be slid from the I and j positions, the optimal state of the searched location (the maximum number of steps that can be slipped) is also found.
When searching for the global optimum, search for each coordinate. If the coordinate has been searched (it is searched at the previous coordinate position), then the optimal state of the coordinate is directly returned. To find the global optimal state.
Code:
# Include <iostream> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 105; int MP [maxn] [maxn]; int step [maxn] [maxn]; int DX [4] = {,-}; int dy [4] = {1,-, 0}; int n, m; void input () {CIN> N> m; For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) CIN> MP [I] [J];} bool OK (INT X, int y) {If (x> = 1 & x <= N & Y> = 1 & Y <= m) return true; return false;} int DFS (int x, int y) {If (step [x] [Y]) return step [x] [Y]; for (INT I = 0; I <4; I ++) {int newx = x + dx [I]; int newy = Y + dy [I]; If (OK (newx, newy) & amp; MP [newx] [newy] <MP [x] [Y]) {int temp = DFS (newx, newy) + 1; // find the longest if (temp> step [x] [Y]) Step [x] [Y] = temp;} return step [x] [Y];} void solve () {int ans =-1; for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) {int temp = DFS (I, j); If (ANS <temp) ans = temp;} cout <ans + 1 <Endl ;} int main () {memset (step, 0, sizeof (STEP); input (); solve (); Return 0 ;}
[ACM] poj 1088 skiing (memory-based search Review)