比赛 20120712 评测结果 AAAAAAAAAA
题目名称 登山 最终得分 100
用户昵称 zhangchi 运行时间 0.042 s
代码语言 Pascal 内存使用 0.18 MiB
提交时间 2012-07-12 10:24:58
显示代码纯文本
var
  n,m,i,j,ans,max:longint;
  map:array[0..61,0..61] of longint;
  p,mark:array[0..61,0..61] of boolean;
  procedure dfs(x,y:longint;q:boolean;step:longint);
  begin
    if q=true then
      if step>ans then ans:=step;
    p[x,y]:=true;
    if q=false then
      begin
        if (x>1)and(map[x-1,y]>map[x,y])and(p[x-1,y]=false) then dfs(x-1,y,q,step+1);
        if (x<n)and(map[x+1,y]>map[x,y])and(p[x+1,y]=false) then dfs(x+1,y,q,step+1);
        if (y>1)and(map[x,y-1]>map[x,y])and(p[x,y-1]=false) then dfs(x,y-1,q,step+1);
        if (y<n)and(map[x,y+1]>map[x,y])and(p[x,y+1]=false) then dfs(x,y+1,q,step+1);
        if step>1 then
          begin
            if (x>1)and(map[x-1,y]<map[x,y])and(p[x-1,y]=false) then dfs(x-1,y,true,step+1);
            if (x<n)and(map[x+1,y]<map[x,y])and(p[x+1,y]=false) then dfs(x+1,y,true,step+1);
            if (y>1)and(map[x,y-1]<map[x,y])and(p[x,y-1]=false) then dfs(x,y-1,true,step+1);
            if (y<n)and(map[x,y+1]<map[x,y])and(p[x,y+1]=false) then dfs(x,y+1,true,step+1);
          end;
      end
    else
      begin
        if (x>1)and(map[x-1,y]<map[x,y])and(p[x-1,y]=false) then dfs(x-1,y,true,step+1);
        if (x<n)and(map[x+1,y]<map[x,y])and(p[x+1,y]=false) then dfs(x+1,y,true,step+1);
        if (y>1)and(map[x,y-1]<map[x,y])and(p[x,y-1]=false) then dfs(x,y-1,true,step+1);
        if (y<n)and(map[x,y+1]<map[x,y])and(p[x,y+1]=false) then dfs(x,y+1,true,step+1);
      end;
    p[x,y]:=false;
  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
      read(map[i,j]);
  for i:=1 to n do
    for j:=1 to m do
      dfs(i,j,false,1);
  writeln(ans);
  close(input); close(output);
end.