【CF676D】Theseus and labyrinth(BFS,最短路)
题意:给定一张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。
1 const dx:array[1..4]of longint=(-1,0,0,1); 2 dy:array[1..4]of longint=(0,-1,1,0); 3 var map:array[1..1000,1..1000]of char; 4 t:array[1..1000,1..1000,1..4]of longint; 5 b:array[1..1000,1..1000,1..4]of longint; 6 d:array[1..4000000]of record 7 x,y,z,s:longint; 8 end; 9 a:array[1..4,1..1000,1..1000,1..4]of longint; 10 n,m,i,j,k,p,sx,sy,ex,ey,ans,t1,t2,t3,t4,x,y:longint; 11 ch:ansistring; 12 13 function min(x,y:longint):longint; 14 begin 15 if x<y then exit(x); 16 exit(y); 17 end; 18 19 procedure bfs; 20 var t,w,x1,y1,ux,uy,uz,i,j,k:longint; 21 begin 22 for i:=1 to n do 23 for j:=1 to m do 24 for k:=1 to 4 do b[i,j,k]:=-1; 25 t:=0; w:=1; d[1].x:=sx; d[1].y:=sy; d[1].z:=1; d[1].s:=0; 26 b[sx,sy,1]:=0; 27 repeat 28 inc(t); 29 ux:=d[t].x; uy:=d[t].y; uz:=d[t].z; 30 for i:=1 to 4 do 31 begin 32 x1:=ux+dx[i]; y1:=uy+dy[i]; 33 if (x1>0)and(x1<=n)and(y1>0)and(y1<=m)and 34 (b[x1,y1,uz]=-1)and(a[uz,ux,uy,i]=1) then 35 begin 36 inc(w); d[w].x:=x1; d[w].y:=y1; d[w].z:=uz; d[w].s:=d[t].s+1; 37 b[x1,y1,uz]:=d[w].s; 38 if (x1=ex)and(y1=ey) then 39 begin 40 ans:=d[w].s; t:=w+100; 41 end; 42 end; 43 end; 44 if b[ux,uy,uz mod 4+1]=-1 then 45 begin 46 inc(w); d[w].x:=ux; d[w].y:=uy; d[w].z:=uz mod 4+1; d[w].s:=d[t].s+1; 47 b[ux,uy,uz mod 4+1]:=d[w].s; 48 end; 49 until t>=w; 50 end; 51 52 53 begin 54 //assign(input,'1.in'); reset(input); 55 //assign(output,'1.out'); rewrite(output); 56 readln(n,m); 57 for i:=1 to n do 58 begin 59 readln(ch); 60 for j:=1 to m do map[i,j]:=ch[j]; 61 end; 62 for p:=1 to 4 do 63 begin 64 if p=1 then 65 for i:=1 to n do 66 for j:=1 to m do 67 begin 68 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; 69 if map[i,j]='-' then begin t[i,j,2]:=1; t[i,j,3]:=1; end; 70 if map[i,j]='|' then begin t[i,j,1]:=1; t[i,j,4]:=1; end; 71 if map[i,j]='^' then t[i,j,1]:=1; 72 if map[i,j]='>' then t[i,j,3]:=1; 73 if map[i,j]='<' then t[i,j,2]:=1; 74 if map[i,j]='v' then t[i,j,4]:=1; 75 if map[i,j]='L' then begin t[i,j,1]:=1; t[i,j,3]:=1; t[i,j,4]:=1; end; 76 if map[i,j]='R' then begin t[i,j,1]:=1; t[i,j,2]:=1; t[i,j,4]:=1; end; 77 if map[i,j]='U' then begin t[i,j,2]:=1; t[i,j,3]:=1; t[i,j,4]:=1; end; 78 if map[i,j]='D' then begin t[i,j,1]:=1; t[i,j,2]:=1; t[i,j,3]:=1; end; 79 end 80 else 81 for i:=1 to n do 82 for j:=1 to m do 83 begin 84 t1:=t[i,j,1]; t2:=t[i,j,2]; t3:=t[i,j,3]; t4:=t[i,j,4]; 85 t[i,j,3]:=t1; t[i,j,4]:=t3; t[i,j,2]:=t4; t[i,j,1]:=t2; 86 end; 87 for i:=1 to n do 88 for j:=1 to m do 89 for k:=1 to 4 do 90 begin 91 x:=i+dx[k]; y:=j+dy[k]; 92 if (x<1)or(x>n)or(y<1)or(y>m) then continue; 93 if t[i,j,k]+t[x,y,5-k]=2 then a[p,i,j,k]:=1; 94 end; 95 end; 96 97 readln(sx,sy); 98 readln(ex,ey); 99 100 if (sx=ex)and(sy=ey) then begin writeln(0); exit; end; 101 ans:=-1; 102 bfs; 103 writeln(ans); 104 105 // close(input); 106 // close(output); 107 end.
null