Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 81099 |
|
Accepted: 30239 |
Description
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 bottom 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 5
16 17 18) 19 6
15 24 25) 20 7
14 23 22) 21 8
13 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
Source
SHTSC 2002 on the Nyoj can be AC, in the POJ has been WA, originally for a two-dimensional array of two size of the same value, is not moved to, (different data reasons)
/*Times memy 79ms 304k by orc*/#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;intR, C;intd[ the][ the], mat[ the][ the];//D[i][j] The longest way to start from the position of column J of Line Iintdir[][2] = {{-1,0},{0,-1},{1,0},{0,1}};intdpintIintj) { //printf ("[%d],[%d],%d\n", i,j,s); if(I <1|| I > R | | J <1|| J > C)return 0; int& res =D[i][j]; if(Res! =-1)returnRes; Res=1; for(intK =0; K <4; ++k) {intTI = i + dir[k][0], TJ = j + dir[k][1]; if(Mat[i][j] > MAT[TI][TJ]) {//here wa a lot of times, mat[i][j] must > MAT[TI][TJ], but not >=res = max (RES,DP (TI,TJ) +1); } } returnRes;}voidGetans () { for(inti =1; I <= R; ++i) for(intj =1; J <= C; ++j) DP (I,J);}intMain () {Ios::sync_with_stdio (0); CIN>> R >>C; for(inti =1; I <= R; ++i) for(intj =1; J <= C; ++j) Cin>>Mat[i][j]; memset (d,-1,sizeofd); Getans (); //D[3][3] = DP (3, 3, mat[3][3]); intAns =0; for(inti =1; I <= R; ++i) for(intj =1; J <= C; ++j) Ans=Max (ans,d[i][j]); cout<< ans <<Endl; // }}
View Code
Skiing (Simple DP)