Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
是不是还有和我一样wa的朋友啊!每次都必须对map赋初值!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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator