记录编号 39505 评测结果 AAAAAAAAAA
题目名称 登山 最终得分 100
用户昵称 Gravatarczp 是否通过 通过
代码语言 Pascal 运行时间 0.083 s
提交时间 2012-07-12 16:15:49 内存使用 0.18 MiB
显示代码纯文本
const dx:array[1..4] of longint=(1,-1,0,0);
      dy:array[1..4] of longint=(0,0,1,-1);
var
 a:array[1..60,1..60] of longint;
 o:array[1..60,1..60] of boolean;
 i,j,m,n,ans,last,ct,pd:longint;
 procedure dfs(x,y,z:longint);
 var i,xx,yy:longint;
 begin
  if (z=1) and (ans>last) then last:=ans;
  inc(ct);
  if ct>10000000 then begin
   writeln(last);
   close(input);close(output);
   halt;
   end;
  if (z=1) and (a[x,y]+ans<=last) then exit;
  for i:=1 to 4 do
   begin
    xx:=x+dx[i];
    yy:=y+dy[i];
    if (xx>0) and (xx<=n) and (yy>0) and (yy<=m) and (not o[xx,yy]) then
     begin
      if (z=0) or (z=-1) then begin
       if a[xx,yy]>a[x,y] then
         begin
               inc(ans);
               o[xx,yy]:=true;
               dfs(xx,yy,0);
               dec(ans);
               o[xx,yy]:=false;
         end;
       if (z<>-1) and (a[xx,yy]<a[x,y]) then
         begin
               inc(ans);
               o[xx,yy]:=true;
               dfs(xx,yy,1);
               dec(ans);
               o[xx,yy]:=false;
         end;
       end;
      if (z=1) and (a[xx,yy]<a[x,y]) then
       begin
        inc(ans);
        o[xx,yy]:=true;
        dfs(xx,yy,1);
        dec(ans);
        o[xx,yy]:=false;
       end;
     end;
   end;
 end;
begin
 assign(input,'hike.in');reset(input);
 assign(output,'hike.out');rewrite(output);
 readln(n,m);
 for i:=1 to n do
  begin
   for j:=1 to m do
    read(a[i,j]);
   readln;
  end;
 ans:=1;
 for i:=1 to n do
  for j:=m downto 1 do
   begin
    o[i,j]:=true;
    dfs(i,j,-1);
    o[i,j]:=false;
   end;
 writeln(last);
 close(input);close(output);
end.