poj3009
样例和网上找的测试数据 都过了 还是 wa
program poj3009;
type
sta=record
x,y:integer;
end;
var
dir:array[1..4,1..2]of integer=((1,0),(-1,0),(0,1),(0,-1));
w,h,ans,ansb:integer;
map:array[0..21,0..21]of integer;
start:sta;
flag:boolean;
procedure dfs(x,y,d,depth:integer);
var
i,tx,ty,j:integer;
begin
if depth>10 then exit;
// chu jie
if map[x,y]=-1 then exit;
//get ans
// if flag then exit;
if map[x,y]=3 then begin
flag:=true;
if ansb<ans then ans:=ansb;
exit;
end;
//hited
if map[x,y]=1 then
begin
map[x,y]:=0;
//tui yi bu
tx:=x-dir[d,1];ty:=y-dir[d,2];
for i:=1 to 4 do
begin
inc(ansb);
dfs(tx+dir[i,1],ty+dir[i,2],i,depth+1);
dec(ansb);
end;
map[x,y]:=1;
end;
// zhi zou
if map[x,y]=0 then dfs(x+dir[d,1],y+dir[d,2],d,depth);
end;
procedure main;
var
i,j,d:integer;
begin
assign(input,'pro.in');
assign(output,'pro.out');
reset(input);
rewrite(output);
readln(w,h);
while ((w<>0)and(h<>0))do
begin
//chu shi hua
for i:=0 to 21 do
for j:=0 to 21 do
map[i,j]:=-1;
ans:=15;
ansb:=1; //the start must
flag:=false;// answer not get
//read map
for i:=1 to h do
begin
for j:=1 to w do
begin
read(map[i,j]);
if map[i,j]=2 then
begin
start.x:=i;
start.y:=j;
map[i,j]:=0;
end;
end;
readln;
end;
//dfs get this ans
for d:=1 to 4 do
if map[start.x+dir[d,1],start.y+dir[d,2]]<>1 then
dfs(start.x+dir[d,1],start.y+dir[d,2],d,1);
//output ans;
if ans<=10 then writeln(ans) else writeln('-1');
//the next map
readln(w,h);
end;
close(input);
close(output);
end;
begin
main;
end.

浙公网安备 33010602011771号