Link: http://poj.org/problem? Id = 1088 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 28508 # Problem/e skiing
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:67899 |
|
Accepted:24981 |
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
Algorithm: greedy ideas:
The positive solution should be dynamic planning + memory-based decision-making.
First, sort the height of each point in ascending order.
Traverse the smallest vertex in sequence to see if there are any higher points around it [up to bottom] than it
Once there is, update the path length of a vertex higher than itNote:: The final output is the path length rather than the starting height of the longest path.
Since the path length is initialized to 0 at the beginning, the final result must be + 1. I did not see it clearly and contributed two wacodes:
/** Train of thought: first sort the heights of each point in ascending order and then traverse them from the smallest point to see if there are any higher vertices around it, update the path length of a vertex higher than it. Note: The final output is the path length rather than the starting height of the longest path. Because the path length is initialized to 0 at the beginning, therefore, the final result must be + 1 */# include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 110; int map [maxn] [maxn]; int DP [maxn] [maxn]; int R, C; int dir [4] [2] = {, 0,-1,-}; struct point {int X, Y; int high ;} P [maxn * maxn]; bool CMP (point a, point B) {return. high <B. high ;} Int judge (int x, int y) {If (x> = 1 & x <= R & Y> = 1 & Y <= c) return 1; else return 0;} void solve (int x, int y) {int NX, NY; int m = 0; For (INT I = 0; I <4; I ++) // The neighboring point is higher than the current point {Nx = x + dir [I] [0]; ny = Y + dir [I] [1]; if (Map [NX] [NY]> map [x] [Y] & judge (NX, NY )) DP [NX] [NY] = max (DP [NX] [NY], DP [x] [Y] + 1) ;}} int main () {While (scanf ("% d", & R, & C )! = EOF) {memset (MAP, 0, sizeof (MAP); memset (DP, 0, sizeof (DP); int n = 0; // Number of vertices for (INT I = 1; I <= r; I ++) {for (Int J = 1; j <= C; j ++) {scanf ("% d", & map [I] [J]); P [N]. X = I; P [N]. y = J; P [N]. high = map [I] [J]; n ++ ;}} sort (p, p + N, CMP ); // sort by height from small to large (INT I = 0; I <n; I ++) {int x = P [I]. x; int y = P [I]. y; solve (x, y);}/* For (INT I = 1; I <= r; I ++) {for (Int J = 1; j <= C; j ++) printf ("% d", DP [I] [J]); printf ("\ n ");} */INT Len = 0; For (INT I = 1; I <= r; I ++) {for (Int J = 1; j <= C; j ++) {If (DP [I] [J]> Len) {Len = DP [I] [J] ;}} printf ("% d \ n ", len + 1);} return 0 ;}