| ||||||||||
| 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的经验不要开标记数组,因为一个点可能由多个方向到达
跟我犯同样错误的同志们注意了……
附ac代码:
var
d:array[1..4,1..2] of longint=((0,1),(1,0),(0,-1),(-1,0));
a:array[0..30,0..30] of integer;
ans,tot,n,m,sx,sy:longint;
procedure init;
var
i,j:longint;
begin
for i:=1 to n do
begin
for j:=1 to m do
begin
read(a[i,j]);
if a[i,j]=2 then
begin
sx:=i;sy:=j;
end;
end;
readln;
end;
end;
procedure dfs(x,y:longint);
var
i,xx,yy,temp1,temp2:longint;
begin
if tot+1>=ans then exit;
for i:=1 to 4 do
begin
xx:=x+d[i,1];
yy:=y+d[i,2];
if (a[xx,yy]=1)or(xx<=0)or(yy<=0)or(xx>n)or(yy>m) then continue;
xx:=x;yy:=y;
while(a[xx,yy]<>1)and(xx>0)and(yy>0)and(xx<=n)and(yy<=m)do
begin
xx:=xx+d[i,1];
yy:=yy+d[i,2];
if a[xx,yy]=3 then
begin
if (tot+1<ans) then ans:=tot+1;
exit;
end;
end;
if (a[xx,yy]=1)then
begin
temp1:=xx-d[i,1]; temp2:=yy-d[i,2];
begin
inc(tot);
a[xx,yy]:=0;
dfs(temp1,temp2);
dec(tot);
a[xx,yy]:=1;
end;
end;
end;
end;
begin
readln(m,n);
while(m<>0)and(n<>0)do
begin
fillchar(a,sizeof(a),0);
init;
ans:=11;
tot:=0;
dfs(sx,sy);
if ans<>11 then
writeln(ans)
else writeln(-1);
readln(m,n)
end;
end.
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator