记录编号 39506 评测结果 AAAAAAAAWA
题目名称 登山 最终得分 90
用户昵称 Gravatarfuhao 是否通过 未通过
代码语言 Pascal 运行时间 0.163 s
提交时间 2012-07-12 16:17:22 内存使用 70.68 MiB
显示代码纯文本
program Hike;
const maxn=61; maxm=61;
      dx:array[1..4] of longint=(1,0,-1,0);
      dy:array[1..4] of longint=(0,1,0,-1);
var
 f:array[0..maxn,0..maxm,0..maxn,0..maxm] of longint;
 v:array[0..maxn,0..maxm] of boolean;
 vv:array[0..maxn,0..maxm,0..maxn,0..maxm] of boolean;
 h:array[0..maxn,0..maxm] of longint;
 a:array[0..maxn*maxm,1..3] of longint;
 n,m,i,j,ans,tot:longint;

 procedure dp(x1,y1,x2,y2:longint);
 var i,t:longint;
 begin
  if vv[x1,y1,x2,y2] then exit;
  vv[x1,y1,x2,y2]:=true; v[x1,y1]:=true; v[x2,y2]:=true;
  t:=0;
  for i:=1 to 4 do
   if (x1+dx[i]>0) and (x1+dx[i]<=n) and
      (y1+dy[i]>0) and (y1+dy[i]<=m) and
      (h[x1,y1]>h[x1+dx[i],y1+dy[i]]) and
      (not v[x1+dx[i],y1+dy[i]]) and
      ((y1+dy[i]<>y2) or (x1+dx[i]<>x2)) then
       begin
        if not vv[x1+dx[i],y1+dy[i],x2,y2] then
         begin
           v[x1+dx[i],y1+dy[i]]:=true;
           dp(x1+dx[i],y1+dy[i],x2,y2);
           v[x1+dx[i],y1+dy[i]]:=false;
         end;
         if f[x1+dx[i],y1+dy[i],x2,y2]>t then
           t:=f[x1+dx[i],y1+dy[i],x2,y2];
       end;
   for i:=1 to 4 do
    if (x2+dx[i]>0) and (x2+dx[i]<=n) and
      (y2+dy[i]>0) and (y2+dy[i]<=m) and
      (h[x2,y2]>h[x2+dx[i],y2+dy[i]]) and
      (not v[x2+dx[i],y2+dy[i]]) and
      ((y2+dy[i]<>y1) or (x2+dx[i]<>x1)) then
       begin
        if not vv[x1,y1,x2+dx[i],y2+dy[i]] then
         begin
          v[x2+dx[i],y2+dy[i]]:=true;
          dp(x1,y1,x2+dx[i],y2+dy[i]);
          v[x2+dx[i],y2+dy[i]]:=false;
         end;
        if f[x1,y1,x2+dx[i],y2+dy[i]]>t then
         t:=f[x1,y1,x2+dx[i],y2+dy[i]];
       end;
   f[x1,y1,x2,y2]:=t+1;
  v[x1,y1]:=false; v[x2,y2]:=false;
 end;

 procedure change(var x,y:longint);
 var t:longint;
 begin
  t:=x; x:=y; y:=t;
 end;

 procedure kp(l,r:longint);
 var i,j,mid:longint;
 begin
  i:=l; j:=r; mid:=a[(l+r) shr 1,3];
  repeat
   while (a[i,3]>mid) do inc(i);
   while (a[j,3]<mid) do dec(j);
   if i<=j then
    begin
     change(a[i,1],a[j,1]); change(a[i,2],a[j,2]); change(a[i,3],a[j,3]);
     inc(i); dec(j);
    end;
  until i>j;
  if i<r then kp(i,r);
  if l<j then kp(l,j);
 end;

begin
 assign(input,'hike.in'); reset(input);
 assign(output,'hike.out'); rewrite(output);
 readln(n,m);
 for i:=1 to n do
  for j:=1 to m do
   begin
    read(h[i,j]);
    inc(tot);
    a[tot,1]:=i; a[tot,2]:=j; a[tot,3]:=h[i,j];
   end;
 kp(1,tot);
 fillchar(v,sizeof(v),#0);
 for i:=tot downto 1 do
  begin
   dp(a[i,1],a[i,2],a[i,1],a[i,2]);
   if ans<f[a[i,1],a[i,2],a[i,1],a[i,2]] then
    ans:=f[a[i,1],a[i,2],a[i,1],a[i,2]];
  end;
 writeln(ans);
 close(input); close(output);
end.