Programming Question # #: Skiing
Source: POJ (Coursera statement: The exercises completed on POJ will not count against the final results of Coursera. )
Note: Total time limit: 1000ms memory limit: 65536kB
Describe
It's not surprising that Michael likes to ski, because skiing is really exciting. But to get the speed, the slippery area must tilt downward, and when you slide to the bottom, you have to go up the slope again or wait for the lift to load you. Michael wants to know the longest landslide in a region. The area is given by a two-dimensional array. Each digit of the array represents the height of the point. Here is an example
1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
One can slide from one point to the next four points adjacent to one another, when and only if the height decreases. In the example above, a sliding landslide is 24-17-16-1. Of course 25-24-23-...-3-2-1 longer. In fact, this is the longest one.
Input
The first line of input represents the number of rows in the range R and the number of columns C (1 <= r,c <= 100). The following are the r lines, each with a C integer representing a height of h,0<=h<=10000.
Output
The length of the longest region of the output.
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
1#include <iostream>2#include <algorithm>3 using namespacestd;4 structDot {5 intRow;6 intcolumn;7 intheight;8 };9Dot matrix[101*101], original[101][101];Ten intcolumn, row; One intlength[101][101]; A - BOOLCompare (dot lhs, dot rhs) { - returnlhs.height<Rhs.height; the } - - intMaxstep (inti) - { + intMaxstep =1; - for(intj =0; J < I; ++j) { + intR =Matrix[j].row; A intc =Matrix[j].column; at inth =Matrix[j].height; - //According to the teacher's "I for everyone" approach - if(R >1&& original[r-1][c].height >h) { -length[r-1][C] = max (length[r-1][C], Length[r][c] +1); - if(length[r-1][C] >maxstep) { -Maxstep = length[r-1][c]; in } - } to if(R < Row && original[r+1][c].height >h) { +length[r+1][C] = max (length[r+1][C], Length[r][c] +1); - if(length[r+1][C] >maxstep) { theMaxstep = length[r+1][c]; * } $ }Panax Notoginseng if(C >1&& original[r][c-1].height >h) { -length[r][c-1] = max (length[r][c-1], Length[r][c] +1); the if(length[r][c-1] >maxstep) { +Maxstep = length[r][c-1]; A } the } + if(C < column && original[r][c+1].height >h) { -length[r][c+1] = max (length[r][c+1], Length[r][c] +1); $ if(length[r][c+1] >maxstep) { $Maxstep = length[r][c+1]; - } - } the } - returnMaxstep;Wuyi } the - intMain () Wu { -Cin>>row>>column; About intK =0; $ for(inti =1; I < row+1; ++i) { - for(intj =1; J < column+1; ++j) { -Matrix[k].row =i; -Matrix[k].column =J; ACin>>Matrix[k].height; +ORIGINAL[I][J] =Matrix[k]; theLENGTH[I][J] =1;//the glide distance of all points is set to 1 first -k++; $ } the } theStd::sort (Matrix, Matrix +k, compare); the intMaxLength =Maxstep (k); thecout<<maxLength; - return 0; in}
POJ Algorithm BASIC Programming question #: Skiing