Back Gear | ski

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.