Advertisement
Guest User

Untitled

a guest
Jan 18th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. uses math;
  2. const
  3. xx : array [1..4] of longint = (1,-1,0,0);
  4. yy : array [1..4] of longint = (0,0,1,-1);
  5.  
  6. var
  7. m:array [1..1000,1..1000] of longint;
  8. a:array [1..1000,1..1000] of longint;
  9. M1,N,i,j:longint;
  10. Ans:longint;
  11. Beg,Fin,e:longint;
  12. st1,st2:array [1..10000000] of longint;
  13.  
  14. Procedure BFS(q,w:longint);
  15. begin
  16. Beg:=1;
  17. Fin:=1;
  18. st1[1]:=q;
  19. st2[1]:=w;
  20. m[q][w]:=1;
  21. While Fin>=Beg do begin
  22. For e:=1 to 4 do begin
  23. if st1[Beg]+xx[e]<=N then
  24. if st1[Beg]+xx[e]>=1 then
  25. if st2[Beg]+yy[e]<=M1 then
  26. if st2[Beg]+yy[e]>=1 then
  27. if a[st1[Beg]][st2[Beg]] + 1 = a[st1[Beg]+xx[e]][st2[Beg]+yy[e]] then
  28. if m[st1[Beg]][st2[Beg]] + 1 > m[st1[Beg]+xx[e]][st2[Beg]+yy[e]] then begin
  29. Inc(Fin);
  30. st1[Fin]:=st1[Beg]+xx[e];
  31. st2[Fin]:=st2[Beg]+yy[e];
  32. m[st1[Fin]][st2[Fin]]:=m[st1[Beg]][st2[Beg]]+1;
  33. end;
  34. end;
  35. Inc(Beg);
  36. end;
  37. end;
  38.  
  39. begin
  40. Assign(input,'sherlocked.in');
  41. Reset(input);
  42. Assign(output,'sherlocked.out');
  43. Rewrite(output);
  44. Read(N,M1);
  45. For i:=1 to N do
  46. For j:=1 to M1 do Read(a[i][j]);
  47. For i:=1 to N do
  48. For j:=1 to M1 do
  49. if m[i][j]=0 then BFS(i,j);
  50. For i:=1 to N do
  51. For j:=1 to M1 do
  52. Ans:=max(Ans,m[i][j]);
  53. Write(Ans);
  54. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement