fork download
  1. uses math;
  2. const maxn = 100;
  3. oo = 200;
  4. var n,low,high,maxa: byte;
  5. a: array[0..maxn+1,0..maxn+1] of byte;
  6. visit: array[0..maxn+1,0..maxn+1] of boolean;
  7. cuoi,giua: byte;
  8. procedure enter;
  9. var i,j,mina:byte;
  10. begin
  11. readln(n);
  12. maxa:=0;
  13. mina:=oo;
  14. for i:=1 to n do
  15. begin
  16. for j:=1 to n do
  17. begin
  18. read(a[i,j]);
  19. if a[i,j]<mina then mina:=a[i,j];
  20. if a[i,j]>maxa then maxa:=a[i,j];
  21. end;
  22. readln;
  23. end;
  24. cuoi:=maxa-mina;
  25. end;
  26. {$M 400000000}
  27. function dfs(x,y:byte):boolean;
  28. var p:boolean;
  29. begin
  30. visit[x,y]:=true;
  31. if (a[x,y]>high) or (a[x,y]<low) then dfs:=false
  32. else
  33. if (x=n) and (y=n) then dfs:=true
  34. else
  35. begin
  36. p:=false;
  37. if (x>1) and (not visit[x-1,y]) then p:=dfs(x-1,y);
  38. if (not p) and (x<n) and (not visit[x+1,y]) then p:=dfs(x+1,y);
  39. if (not p) and (y>1) and (not visit[x,y-1]) then p:=dfs(x,y-1);
  40. if (not p) and (y<n) and (not visit[x,y+1]) then p:=dfs(x,y+1);
  41. dfs:=p;
  42. end;
  43. end;
  44. function check:boolean;
  45. var p:boolean;
  46. i,j:byte;
  47. begin
  48. p:=false;
  49. for low:=0 to maxa-giua do
  50. begin
  51. high:=low+giua;
  52. for i:=1 to n do
  53. for j:=1 to n do
  54. visit[i,j]:=false;
  55. if dfs(1,1) then
  56. begin
  57. p:=true;
  58. break;
  59. end;
  60. end;
  61. check:=p;
  62. end;
  63. procedure tknp;
  64. var i,j,dau: byte;
  65. begin
  66. dau:=0;
  67. for giua:=dau to cuoi do
  68. begin
  69. if check then break;
  70. end;
  71. writeln(giua);
  72. end;
  73. begin
  74. enter;
  75. tknp;
  76. end.
  77.  
Success #stdin #stdout 0.01s 468KB
stdin
13
20	4	27	47	19	69	80	75	64	34	101	55	15	
2	65	85	35	71	94	93	82	56	109	18	25	36	
19	11	103	2	54	92	41	58	89	74	63	91	74	
109	107	10	5	93	104	103	28	108	50	84	78	99	
91	33	44	37	59	32	48	109	69	84	79	5	30	
59	71	37	60	78	94	107	21	102	4	87	92	17	
95	30	104	44	106	104	105	78	66	74	60	59	76	
66	3	46	46	11	63	57	9	13	46	66	70	44	
47	9	41	66	87	104	98	22	15	95	89	93	30	
19	10	37	75	98	100	45	108	67	6	13	92	45	
70	100	37	55	48	102	75	107	80	7	78	34	21	
86	74	81	24	33	99	36	59	105	38	65	52	19	
22	34	97	63	36	84	28	51	34	4	18	78	78	
stdout
76