Description
It's not surprising that Michael likes skiing, because skiing is really exciting. But in order to get the speed, the slippery area must be tilted downward, and when you slide to the bottom of the slope, you have to go uphill again or wait for the lift to load you. Michael wants to know the longest bottom slide in a region. The region 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
A person can slide from one point to the next four or so adjacent points, when and only if the height is reduced. 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 you enter represents the number of rows in the range R and the number of columns C (1 <=r,c <=100). Here are the r rows, each with a C integer representing a height of h,0<=h<=10000.
Output
The length of the longest area of output.
Sampleinput
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
Sampleoutput
25
Ideas for solving problems:
F[I,J] represents the maximum length until [i,j], and the state transfer equation is:
f[xx,yy]:=f[a[i].x,a[i].y]+1;
Time complexity O (n㏒n)
Program:
Const MAXN=500;
WAY:ARRAY[1..4,1..2] oflongint= (( -1,0), (1,0), (0,-1), (0,1));
Type Arr=record X,y,s:integer;
End
var map,f:array[1..maxn,1..maxn] of Integer;
A:ARRAY[1..MAXN*MAXN] of arr;
R,c,sum,i,j,k,xx,yy:longint;
Procedure Qsort (L,r:longint);
var i,j,mid:longint;
T:arr;
Begin if L>=rthen exit; I:=l;
j:=r;mid:=a[(l+r) div 2].s;
Repeat while A[I].S while A[j].s>mid does Dec (j);
If I<=j then begin t:=a[i];
A[I]:=A[J];
a[j]:=t;
Inc (I);
Dec (j);
End
untili>=j;
Qsort (L,J);
Qsort (I,R);
End
Begin READLN (R,C);
k:=0;
sum:=0;
For I:=1 to R doing for J:=1 TOC do begin read (MAP[I,J));
Inc (k);
A[k].x:=i;
A[k].y:=j;
A[K].S:=MAP[I,J];
End
Qsort (1,K);
For I:=1 to K does for J:=1 to4 does begin xx:=a[i].x+way[j,1];
yy:=a[i].y+way[j,2]; if (xx>0) and (Xx<=r) and (yy>0) and (YY<=C) then if (F[a[i].x,a[i].y]+1>f[xx,yy)) and (Map[a[i].x,a[i].y] begin F[XX,YY
]:=f[a[i].x,a[i].y]+1;
If F[xx,yy]>sum then Sum:=f[xx,yy];
End
End
Writeln (sum+1); End.
Copyright belongs to: Chris
Original address: http://blog.sina.com.cn/s/blog_83ac6af80102vjrv.html
Reprint must be linked to the form of the original source and this statement.