3299: [USACO2011 open]corn Maze corn maze time limit:10 Sec Memory limit:128 MB
submit:137 solved:59
[Submit] [Status] [Discuss] Description
This fall, John took the cows to the corn maze. Maze can be divided into nxm lattice, some lattice planted corn, species of corn lattice can not pass.
The maze of the four boundaries are planted corn lattice, its che only one lattice, it is the export.
In this maze, there are some magical teleport points. 6 Each transfer point is made up of a pair of points, and once you walk into a node of the delivery point,
The machine will force you to take you to the other end of the delivery point. All the transfer points are bidirectional, and if you set the other end, the machine will send you back.
A cow can only move one cell in the next four directions in a single unit of time, but the transfer is instantaneous.
Now W West lost in the maze, she only know where the current bit move place, please help her to use the shortest time out of the maze it.
Input
First line: Two integers separated by a space: N and m,2
The second line to N+1 line: line I+1 has m consecutive characters, describes the maze line I information. Where "#" stands for the Impassable Corn land,
"." Represents the accessible meadow, "@" stands for Bessie's starting bit move place, "=" stands for the maze exit,
Capital letters "A" to "Z" always appear in pairs, representing a pair of transfer points
Output
First line: An integer that shows Bessie's shortest time out of the maze, guaranteeing that the escape route must exist
Sample Input
3 p
###=##
#. w.##
#.####
#[email protected]##
######
Sample Output3
HINT
From the beginning to the right, through W, then out the maze from the other end.
Source
Silver
Problem: Have done countless BFS maze water problems, but this is a bit more magical ... In fact, it is the instantaneous transmission between multiple transmission points, and nothing else.
1 ConstDd:Array[1..4,1..2] ofLongint= ((1,0),(-1,0),(0,-1),(0,1));2 var3 I,j,k,l,m,n,x0,y0,x1,y1,x,y,f,r:longint;4B:Array[0.. +,0.. +] ofLongint;5Tr:Array[2.. -,1..4] ofLongint;6D:Array[0..1000000,1..2] ofLongint;7 Ch:char;8 procedureTrans (z:longint;varx,y:longint);9 beginTen if(z<2)or(z> -) Thenexit; One if(Tr[z,1]=X) and(Tr[z,2]=y) Then A begin -X:=tr[z,3];y:=tr[z,4]; - End the Else if(Tr[z,3]=X) and(Tr[z,4]=y) Then - begin -X:=tr[z,1];y:=tr[z,2]; - End; + End; - begin + readln (n,m); AFillchar (A,sizeof (a),-1); atFillchar (tr,sizeof (TR),0); - fori:=1 toN Do - begin - forj:=1 toM Do - begin - read (CH); in CaseUpCase (CH) of - '#': a[i,j]:=1; to '.': a[i,j]:=0; + '=':begin -x1:=i;y1:=J; thea[i,j]:=0; * End; $ '@':beginPanax Notoginsengx0:=i;y0:=J; -a[i,j]:=1; the End; + 'A'..'Z':begin AA[i,j]:=ord (CH)- the; the ifTR[A[I,J],1]=0 Then + begin -TR[A[I,J],1]:=i; $TR[A[I,J],2]:=J; $ End - Else - begin theTR[A[I,J],3]:=i; -TR[A[I,J],4]:=J;Wuyi End; the End; - End; Wu End; - Readln; About End; $f:=1; r:=2;d [1,1]:=x0;d[1,2]:=y0;b[x0,y0]:=1; - whileF<r Do - begin - fori:=1 to 4 Do A begin +X:=d[f,1]+dd[i,1]; theY:=d[f,2]+dd[i,2]; - if(x<1)or(x>n)or(y<1)or(y>m) Thencontinue; $ ifABS (A[x,y]) =1 Thencontinue; the Trans (a[x,y],x,y); the ifb[x,y]=0 Then the begin theB[x,y]:=b[d[f,1],d[f,2]]+1; -D[r,1]:=x;d[r,2]:=y; in if(X=X1) and(y=y1) Then the begin theWriteln (b[x,y]-1); About Halt; the End; the Inc (R); the End; + End; - Inc (f); the End;Bayi End.
3299: [USACO2011 open]corn Maze Corn Maze