Background
Chengcheng the third problem of the first simulation game
Describe
TRS likes to ski. He came to a ski resort where the ski was a rectangle, and for the sake of simplicity, we used the matrix of the R row C column to represent each piece of terrain. To get a faster speed, the glide route must be tilted downward.
For example, the rectangle in the sample, you can slide from one point to the top or bottom four adjacent points. For example 24-17-16-1, in fact 25-24-23 ... 3-2-1 longer, in fact this is the longest one.
Input file
Line 1th: Two digits r,c (1<=r,c<=100), representing the rows and columns of a matrix.
2nd.. R+1 Line: The number of c per line, indicating the matrix.
Output file
Only one row: outputs 1 integers representing the maximum length that can be slid.
Test Sample 1
Input
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
Output
25
Topic Analysis: The map is small, enumerate the starting point, with DFS enumerate the longest path can be AC.
Source:
ConstDX:Array[1..4] ofInteger= (1,-1,0,0); DY:Array[1..4] ofInteger= (0,0,1,-1);varN,m,i,j,max,ans,t:longint; A,f:Array[0.. -,0.. -] ofLongint;functionDFS (x,y:longint): Longint;varI,xx,yy,t,ans:longint;beginans:=0; ifF[x,y]<>0 Thenexit (F[x,y]); fori:=1 to 4 Do beginXX:=x+Dx[i]; YY:=y+Dy[i]; if(xx>=1) and(xx<=n) and(yy>=1) and(yy<=m) and(A[xx,yy]beginT:=DFS (XX,YY); ifT>ans Thenans:=T; End; End; F[x,y]:=ans+1; Exit (ans+1);End;beginreadln (n,m); fori:=1 toN Do forj:=1 toM Doread (a[i,j]); fori:=1 toN Do begin forj:=1 toM Do beginans:=DFS (I,J); ifAns>max Thenmax:=ans; End; End; Writeln (max);End.
Back Gear | ski