Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uses math;
- const
- xx : array [1..4] of longint = (1,-1,0,0);
- yy : array [1..4] of longint = (0,0,1,-1);
- var
- m:array [1..1000,1..1000] of longint;
- a:array [1..1000,1..1000] of longint;
- M1,N,i,j:longint;
- Ans:longint;
- Beg,Fin,e:longint;
- st1,st2:array [1..10000000] of longint;
- Procedure BFS(q,w:longint);
- begin
- Beg:=1;
- Fin:=1;
- st1[1]:=q;
- st2[1]:=w;
- m[q][w]:=1;
- While Fin>=Beg do begin
- For e:=1 to 4 do begin
- if st1[Beg]+xx[e]<=N then
- if st1[Beg]+xx[e]>=1 then
- if st2[Beg]+yy[e]<=M1 then
- if st2[Beg]+yy[e]>=1 then
- if a[st1[Beg]][st2[Beg]] + 1 = a[st1[Beg]+xx[e]][st2[Beg]+yy[e]] then
- if m[st1[Beg]][st2[Beg]] + 1 > m[st1[Beg]+xx[e]][st2[Beg]+yy[e]] then begin
- Inc(Fin);
- st1[Fin]:=st1[Beg]+xx[e];
- st2[Fin]:=st2[Beg]+yy[e];
- m[st1[Fin]][st2[Fin]]:=m[st1[Beg]][st2[Beg]]+1;
- end;
- end;
- Inc(Beg);
- end;
- end;
- begin
- Assign(input,'sherlocked.in');
- Reset(input);
- Assign(output,'sherlocked.out');
- Rewrite(output);
- Read(N,M1);
- For i:=1 to N do
- For j:=1 to M1 do Read(a[i][j]);
- For i:=1 to N do
- For j:=1 to M1 do
- if m[i][j]=0 then BFS(i,j);
- For i:=1 to N do
- For j:=1 to M1 do
- Ans:=max(Ans,m[i][j]);
- Write(Ans);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement