| ||||||||||
| 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 | |||||||||
普通宽搜+specialtype
rec=record
px,py,bx,by,step:longint;
end;
const
d:array[1..4,1..2]of longint=((-1,0),(0,1),(1,0),(0,-1));
var
n,i,j,k,stx,sty,ans,l,r:longint;
map:array[0..26,0..26]of char;
v:array[1..25,1..25,1..25,1..25]of longint;
q:array[1..1000000]of rec;
procedure findst;
begin
if map[i,j]='*' then
begin
stx:=i;sty:=j;
end;
end;
procedure init_renew;
var
ch:char;
begin
fillchar(v,sizeof(v),255);
readln(n);
for i:=0 to n+1 do
for j:=0 to n+1 do map[i,j]:='#';
for i:=1 to n do
begin
for j:=1 to n do begin read(map[i,j]);findst;end;
readln;
end;
end;
procedure bfs;
var
nx,ny:longint;
begin
l:=1;
for i:=1 to 4 do
begin
nx:=stx+d[i,1];ny:=sty+d[i,2];
if map[nx,ny]='.' then
begin
inc(r);
with q[r] do
begin
px:=nx;py:=ny;
bx:=stx;by:=sty;
end;
v[nx,ny,stx,sty]:=0;
end;
end;
while l<=r do
begin
for i:=1 to 4 do
begin
nx:=q[l].px+d[i,1];
ny:=q[l].py+d[i,2];
if (map[nx,ny]<>'#')and((nx<>q[l].bx)or(ny<>q[l].by))
and(v[nx,ny,q[l].bx,q[l].by]=-1) then
begin
inc(r);
with q[r] do
begin
px:=nx;py:=ny;
bx:=q[l].bx;by:=q[l].by;
step:=q[l].step+1;
v[px,py,bx,by]:=step;
end;
if q[r].step>ans then ans:=q[r].step;
end;
end;
for i:=1 to 4 do with q[l] do
begin
if (px+d[i,1]=bx)and(py+d[i,2]=by)and
(map[px-d[i,1],py-d[i,2]]<>'#')and
(v[px-d[i,1],py-d[i,2],px,py]=-1) then
begin
inc(r);
with q[r] do
begin
px:=q[l].px-d[i,1];
py:=q[l].py-d[i,2];
bx:=q[l].px;
by:=q[l].py;
step:=q[l].step+1;
v[px,py,bx,by]:=step;
end;
if q[r].step>ans then ans:=q[r].step;
end;
end;
inc(l);
end;
end;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;
procedure special;
var
i,j,k,l:longint;
begin
ans:=0;
for i:=1 to n do
for j:=1 to n do
for k:=1 to n do
for l:=1 to n do
begin
if (k=stx)and(l=sty) then v[i,j,k,l]:=0;
ans:=max(ans,v[i,j,k,l]);
end;
end;
begin
init_renew;
bfs;
special;
writeln(ans);
end.
special过程的用处是,判断宽搜到得某个状态,是否就是箱子在终点的状态。如果是,那么这个搜到的状态值就应该是0。
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator