第一和第九个点总是过不去!
program tyvj_1004;
const
d:array[1..2,1..8] of integer=((-1,-1,-1,0,0,1,1,1),
(-1,0,1,-1,1,-1,0,1));
inf=maxint;
type
node=record
a,x,y:integer;
end;
var
n,m,i,j,k,max:integer;
g,f:array[0..100,0..100] of integer;
a:array[1..10005] of node;
procedure qsort(l,r:integer);
var
i,j,k,mid:integer;
t:node;
begin
i:=l; j:=r; k:=random(r-l)+l;
mid:=a[k].a;
repeat
while a[i].a<mid do inc(i);
while a[j].a>mid do dec(j);
if i<=j then begin
t:=a[i]; a[i]:=a[j]; a[j]:=t;
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
begin
//assign(input,'a.in'); reset(input);
readln(n,m);
for i:=0 to n+1 do for j:=0 to m+1 do g[i,j]:=inf;
for i:=1 to n do
for j:=1 to m do
begin
read(a[(i-1)*m+j].a);
a[(i-1)*m+j].x:=i;
a[(i-1)*m+j].y:=j;
g[i,j]:=a[(i-1)*m+j].a;
end;
randomize; qsort(1,n*m);
fillchar(f,sizeof(f),0);
for i:=1 to n*m do
begin
max:=-inf;
for k:=1 to 8 do
if g[a[i].x+d[1,k],a[i].y+d[2,k]]<a[i].a then
if f[a[i].x+d[1,k],a[i].y+d[2,k]]>max then
max:=f[a[i].x+d[1,k],a[i].y+d[2,k]];
if max=-inf then max:=0;
f[a[i].x,a[i].y]:=max+1;
end;
max:=-inf;
for i:=1 to n do for j:=1 to m do if f[i,j]>max then max:=f[i,j];
writeln(max);
end.
program tyvj_1004;
const
d:array[1..2,1..8] of integer=((-1,-1,-1,0,0,1,1,1),
(-1,0,1,-1,1,-1,0,1));
inf=maxint;
type
node=record
a,x,y:integer;
end;
var
n,m,i,j,k,max:integer;
g,f:array[0..100,0..100] of integer;
a:array[1..10005] of node;
procedure qsort(l,r:integer);
var
i,j,k,mid:integer;
t:node;
begin
i:=l; j:=r; k:=random(r-l)+l;
mid:=a[k].a;
repeat
while a[i].a<mid do inc(i);
while a[j].a>mid do dec(j);
if i<=j then begin
t:=a[i]; a[i]:=a[j]; a[j]:=t;
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
begin
//assign(input,'a.in'); reset(input);
readln(n,m);
for i:=0 to n+1 do for j:=0 to m+1 do g[i,j]:=inf;
for i:=1 to n do
for j:=1 to m do
begin
read(a[(i-1)*m+j].a);
a[(i-1)*m+j].x:=i;
a[(i-1)*m+j].y:=j;
g[i,j]:=a[(i-1)*m+j].a;
end;
randomize; qsort(1,n*m);
fillchar(f,sizeof(f),0);
for i:=1 to n*m do
begin
max:=-inf;
for k:=1 to 8 do
if g[a[i].x+d[1,k],a[i].y+d[2,k]]<a[i].a then
if f[a[i].x+d[1,k],a[i].y+d[2,k]]>max then
max:=f[a[i].x+d[1,k],a[i].y+d[2,k]];
if max=-inf then max:=0;
f[a[i].x,a[i].y]:=max+1;
end;
max:=-inf;
for i:=1 to n do for j:=1 to m do if f[i,j]>max then max:=f[i,j];
writeln(max);
end.