您的位置:首页 > 其它

【CF676D】Theseus and labyrinth(BFS,最短路)

2016-09-25 20:52 387 查看

题意:给定一张N*M的地图,每一格都是一个房间,房间之间有门。每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等。现在给出起点和终点,每次你可以把全部房间旋转90度或者移动到相邻的房间,但前提是两个房间之间都有有门,现在要你求起点出发到终点的最少时间。

  • «+» means this block has 4 doors (one door to each neighbouring block);
  • «-» means this block has 2 doors — to the left and to the right neighbours;
  • «|» means this block has 2 doors — to the top and to the bottom neighbours;
  • «^» means this block has 1 door — to the top neighbour;
  • «>» means this block has 1 door — to the right neighbour;
  • «<» means this block has 1 door — to the left neighbour;
  • «v» means this block has 1 door — to the bottom neighbour;
  • «L» means this block has 3 doors — to all neighbours except left one;
  • «R» means this block has 3 doors — to all neighbours except right one;
  • «U» means this block has 3 doors — to all neighbours except top one;
  • «D» means this block has 3 doors — to all neighbours except bottom one;
  • «*» means this block is a wall and has no doors.

思路:预处理方格间的联通情况,随后BFS。注意起点与终点相同的情况ANS=0。

const dx:array[1..4]of longint=(-1,0,0,1);
dy:array[1..4]of longint=(0,-1,1,0);
var map:array[1..1000,1..1000]of char;
t:array[1..1000,1..1000,1..4]of longint;
b:array[1..1000,1..1000,1..4]of longint;
d:array[1..4000000]of record
x,y,z,s:longint;
end;
a:array[1..4,1..1000,1..1000,1..4]of longint;
n,m,i,j,k,p,sx,sy,ex,ey,ans,t1,t2,t3,t4,x,y:longint;
ch:ansistring;

function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end;

procedure bfs;
var t,w,x1,y1,ux,uy,uz,i,j,k:longint;
begin
for i:=1 to n do
for j:=1 to m do
for k:=1 to 4 do b[i,j,k]:=-1;
t:=0; w:=1; d[1].x:=sx; d[1].y:=sy; d[1].z:=1; d[1].s:=0;
b[sx,sy,1]:=0;
repeat
inc(t);
ux:=d[t].x; uy:=d[t].y; uz:=d[t].z;
for i:=1 to 4 do
begin
x1:=ux+dx[i]; y1:=uy+dy[i];
if (x1>0)and(x1<=n)and(y1>0)and(y1<=m)and
(b[x1,y1,uz]=-1)and(a[uz,ux,uy,i]=1) then
begin
inc(w); d[w].x:=x1; d[w].y:=y1; d[w].z:=uz; d[w].s:=d[t].s+1;
b[x1,y1,uz]:=d[w].s;
if (x1=ex)and(y1=ey) then
begin
ans:=d[w].s; t:=w+100;
end;
end;
end;
if b[ux,uy,uz mod 4+1]=-1 then
begin
inc(w); d[w].x:=ux; d[w].y:=uy; d[w].z:=uz mod 4+1; d[w].s:=d[t].s+1;
b[ux,uy,uz mod 4+1]:=d[w].s;
end;
until t>=w;
end;

begin
//assign(input,'1.in'); reset(input);
//assign(output,'1.out'); rewrite(output);
readln(n,m);
for i:=1 to n do
begin
readln(ch);
for j:=1 to m do map[i,j]:=ch[j];
end;
for p:=1 to 4 do
begin
if p=1 then
for i:=1 to n do
for j:=1 to m do
begin
if map[i,j]='+' then begin t[i,j,1]:=1; t[i,j,2]:=1; t[i,j,3]:=1; t[i,j,4]:=1; end;
if map[i,j]='-' then begin t[i,j,2]:=1; t[i,j,3]:=1; end;
if map[i,j]='|' then begin t[i,j,1]:=1; t[i,j,4]:=1; end;
if map[i,j]='^' then t[i,j,1]:=1;
if map[i,j]='>' then t[i,j,3]:=1;
if map[i,j]='<' then t[i,j,2]:=1;
if map[i,j]='v' then t[i,j,4]:=1;
if map[i,j]='L' then begin t[i,j,1]:=1; t[i,j,3]:=1; t[i,j,4]:=1; end;
if map[i,j]='R' then begin t[i,j,1]:=1; t[i,j,2]:=1; t[i,j,4]:=1; end;
if map[i,j]='U' then begin t[i,j,2]:=1; t[i,j,3]:=1; t[i,j,4]:=1; end;
if map[i,j]='D' then begin t[i,j,1]:=1; t[i,j,2]:=1; t[i,j,3]:=1; end;
end
else
for i:=1 to n do
for j:=1 to m do
begin
t1:=t[i,j,1]; t2:=t[i,j,2]; t3:=t[i,j,3]; t4:=t[i,j,4];
t[i,j,3]:=t1; t[i,j,4]:=t3; t[i,j,2]:=t4; t[i,j,1]:=t2;
end;
for i:=1 to n do
for j:=1 to m do
for k:=1 to 4 do
begin
x:=i+dx[k]; y:=j+dy[k];
if (x<1)or(x>n)or(y<1)or(y>m) then continue;
if t[i,j,k]+t[x,y,5-k]=2 then a[p,i,j,k]:=1;
end;
end;

readln(sx,sy);
readln(ex,ey);

if (sx=ex)and(sy=ey) then begin writeln(0); exit; end;
ans:=-1;
bfs;
writeln(ans);

// close(input);
// close(output);
end.

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: