Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

是不是还有和我一样wa的朋友啊!每次都必须对map赋初值!

Posted by maxingc at 2011-10-09 21:44:20 on Problem 3009
const
  bx:array[0..4] of integer=(0,-1,1,0,0);
  by:array[0..4] of integer=(0,0,0,-1,1);
var
  i,j,k,n,m:Longint;
  map:array[0..21,0..21] of longint;
  sx,sy,ans:longint;
procedure init;
  begin
    fillchar(map,sizeof(map),0);//不加这句,无限wa
    for i:=1 to n do
      for j:=1 to m do
       begin
        read(map[i,j]);
        if map[i,j]=2 then
          begin
            sx:=i;
            sy:=j;
            map[i,j]:=0;
          end;

       end;
  end;
procedure dfs(x,y,k:longint);
  var
    xx,yy,i,xxx,yyy:Longint;
  begin
        if k>10 then exit;
        for i:=1 to 4 do
          begin
            xxx:=x+bx[i];
            yyy:=y+by[i];
            if (xxx<1) or (xxx>n) or (yyy<1) or (yyy>m) then continue;
            if map[xxx,yyy]=1 then continue;
             if map[xxx,yyy]=3 then
                     begin
                       if k+1<ans then ans:=k+1;
                      break;
                     end;
            while map[xxx,yyy]<>1 do
              begin
                xxx:=xxx+bx[i];
                yyy:=yyy+by[i];
                if map[xxx,yyy]=3 then
                     begin
                       if k+1<ans then ans:=k+1;
                      break;
                     end;
                if (xxx<1) or (xxx>n) or (yyy<1) or (yyy>m) then break;
              end;
            if map[xxx,yyy]=1 then
              begin
                map[xxx,yyy]:=0;
                dfs(xxx-bx[i],yyy-by[i],k+1);
                map[xxx,yyy]:=1;
              end;
          end;

      end;
procedure main;
  begin
    ans:=11;
    dfs(sx,sy,0);
    if ans=11 then writeln(-1) else writeln(ans);
  end;
begin
  assign(input,'poj3009.in');reset(input);
  assign(output,'poj3009.out');rewrite(output);
  readln(m,n);
  while (m+n<>0) do
  begin
  init;
  main;
  readln(m,n);
  end;
  close(input);
  close(output);
end.


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator