| ||||||||||
| 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 | |||||||||
搜索题头一次一次AC啊~ 弱菜非常激动~哈~ 不过是单向广搜 慢了点啊
Const
l=500;
l2=10000;
imp=10000000;
a:array [0..4,0..1] of integer=((0,0),(1,0),(0,-1),(-1,0),(0,1));
Type
jl=record
x,y,f:longint;
End;
Var
i,j,k,m,n,head,last:longint;
dx,df,dy,mx,my,uf,ux,uy:longint;
stop:array [1..l,1..l] of boolean;
indl:array [1..l,1..l,0..4] of boolean;
step:array [1..l,1..l,0..4] of longint;
easybroken:array [1..l,1..l] of boolean;
dl:array [0..l2] of jl;
Procedure add(hx,hy,hf,hs:longint);
Var
hx2,hy2,hf2:longint;
Begin
step[hx,hy,hf] := hs;
If not indl[hx,hy,hf] then Begin
last := last+1;
If last=l2 then last := 0;
dl[last].x := hx;
dl[last].y := hy;
dl[last].f := hf;
indl[hx,hy,hf] := true;
End;
hx2 := hx+a[hf,0];
hy2 := hy+a[hf,1];
Case hf of
1:hf2 := 3;
2:hf2 := 4;
3:hf2 := 1;
4:hf2 := 2;
Else hf2 := 0;
End;
If hf2>0 then step[hx2,hy2,hf2] := hs;
End;
Procedure init;
Var
s:ansistring;
sx1,sy1,sf:longint;
Begin
Readln(m,n);
If m=0 then Halt;
sx1 := 0;
sy1 := 0;
sf := 0;
Fillchar(stop,sizeof(stop),0);
Fillchar(indl,sizeof(indl),0);
Fillchar(easybroken,sizeof(easybroken),0);
Fillchar(dl,sizeof(dl),0);
For i := 1 to m do For j := 1 to n do For k := 0 to 4 do step[i,j,k] := imp;
For i := 1 to m do Begin
Readln(s);
For j := 1 to n do
Case s[j] of
'#':stop[i,j] := true;
'O':Begin
mx := i;
my := j;
End;
'X':Begin
If sx1=0 then Begin
sx1 := i;
sy1 := j;
End
Else Begin
If i>sx1 then sf := 1
Else If i<sx1 then sf := 3
Else If j>sy1 then sf := 4
Else sf := 2;
End;
End;
'E':easybroken[i,j] := true;
End;
End;
head := -1;
last := -1;
add(sx1,sy1,sf,0);
End;
Procedure bfs;
Var
nowstep:longint;
ux2,uy2:longint;
Begin
While head<>last do Begin
head := head+1;
If head=l2 then head := 0;
dx := dl[head].x;
dy := dl[head].y;
df := dl[head].f;
nowstep := step[dx,dy,df];
If df=0 then Begin
If (dx=mx)and(dy=my) then Continue;
For i := 1 to 4 do Begin
ux := dx+a[i,0];
uy := dy+a[i,1];
ux2 := ux+a[i,0];
uy2 := uy+a[i,1];
uf := i;
If (ux<1)or(uy<1)or(ux2<1)or(uy2<1)or(ux>m)or(ux2>m)or(uy>n)or(uy2>n) then Continue;
If (stop[ux,uy])or(stop[ux2,uy2]) then Continue;
If step[ux,uy,uf]<=nowstep+1 then Continue;
add(ux,uy,uf,nowstep+1);
End;
End
Else Begin
If df=3 then Begin
dx := dx+a[3,0];
dy := dy+a[3,1];
df := 1;
End;
If df=4 then Begin
dx := dx+a[4,0];
dy := dy+a[4,1];
df := 2;
End;
If df=1 then Begin
For i := 1 to 4 do Begin
ux := dx+a[i,0];
uy := dy+a[i,1];
If i=df then Begin
ux := ux+a[i,0];
uy := uy+a[i,1];
End;
If (i=1)or(i=3) then Begin
ux2 := ux;
uy2 := uy;
uf := 0;
End
Else Begin
ux2 := ux+a[df,0];
uy2 := uy+a[df,1];
uf := df;
End;
If (ux<1)or(uy<1)or(ux2<1)or(uy2<1)or(ux>m)or(ux2>m)or(uy>n)or(uy2>n) then Continue;
If (ux=ux2)and(uy=uy2)and(easybroken[ux,uy]) then Continue;
If (stop[ux,uy])or(stop[ux2,uy2]) then Continue;
If (step[ux,uy,uf]<=nowstep+1) then Continue;
add(ux,uy,uf,nowstep+1);
End;
End
Else Begin
For i := 1 to 4 do Begin
ux := dx+a[i,0];
uy := dy+a[i,1];
If i=df then Begin
ux := ux+a[i,0];
uy := uy+a[i,1];
End;
If (i=2)or(i=4) then Begin
ux2 := ux;
uy2 := uy;
uf := 0;
End
Else Begin
ux2 := ux+a[df,0];
uy2 := uy+a[df,1];
uf := df;
End;
If (ux<1)or(uy<1)or(ux2<1)or(uy2<1)or(ux>m)or(ux2>m)or(uy>n)or(uy2>n) then Continue;
If (ux=ux2)and(uy=uy2)and(easybroken[ux,uy]) then Continue;
If (stop[ux,uy])or(stop[ux2,uy2]) then Continue;
If (step[ux,uy,uf]<=nowstep+1) then Continue;
add(ux,uy,uf,nowstep+1);
End;
End;
End;
indl[dx,dy,df] := false;
End;
End;
Begin
While true do Begin
init;
bfs;
If step[mx,my,0]=imp then Writeln('Impossible')
Else Writeln(step[mx,my,0]);
End;
End.
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator