Codeforces Round #354 (Div. 2) D (BFS)

网友投稿 808 2022-10-21

Codeforces Round #354 (Div. 2) D (BFS)

Codeforces Round #354 (Div. 2) D (BFS)

D. Theseus and labyrinth

D. Theseus and labyrinth

time limit per test 3 seconds

memory limit per test 256 megabytes

input standard input

output standard output

Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size n × m and consists of blocks of size 1 × 1.

Each block of the labyrinth has a button that rotates all blocks 90 degrees clockwise. Each block rotates around its center and doesn't change its position in the labyrinth. Also, each block has some number of doors (possibly none). In one minute, Theseus can either push the button in order to rotate all the blocks 90 degrees clockwise or pass to the neighbouring block. Theseus can go from block A to some neighbouring block B only if block A has a door that leads to block B and block B has a door that leads to block A.

Theseus found an entrance to labyrinth and is now located in block (xT, yT) — the block in the row xT and column yT. Theseus know that the Minotaur is hiding in block (xM, yM)

Theseus is a hero, not a programmer, so he asks you to help him.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in labyrinth, respectively.

Each of the following n lines contains m

«+» means this block has 4«-» means this block has 2«|» means this block has 2«^» means this block has 1«>» means this block has 1«<» means this block has 1«v» means this block has 1«L» means this block has 3«R» means this block has 3«U» means this block has 3«D» means this block has 3«*» means this block is a wall and has no doors.

Left, right, top and bottom are defined from representing labyrinth as a table, where rows are numbered from 1 to n from top to bottom and columns are numbered from 1 to m

Next line contains two integers — coordinates of the block (xT, yT) (1 ≤ xT ≤ n, 1 ≤ yT ≤ m), where Theseus is initially located.

Last line contains two integers — coordinates of the block (xM, yM) (1 ≤ xM ≤ n, 1 ≤ yM ≤ m), where Minotaur hides.

It's guaranteed that both the block where Theseus starts and the block where Minotaur is hiding have at least one door. Theseus and Minotaur may be initially located at the same block.

Output

If Theseus is not able to get to Minotaur, then print -1

Examples

input

2 2+**U1 12 2

output

-1

input

2 3<><><>1 12 1

output

4

Note

Assume that Theseus starts at the block (xT, yT) at the moment 0.

题解;

在一幅n*m的地图,地图上有很多标志,然后每秒钟可以穿过门,也可以使得所有门都顺时针转动90°

如果你要从A到B,那么从B也必须能够到达A,问从起点到终点的最短时间是多少?

BFS撸一波。。

AC代码

#includeusing namespace std;typedef long long ll;const int N=1000+5;const int inf=1e9;char s[4][N][N];int d[4][N][N],n,m;bool vis[4][N][N];int dx[]={0,1,0,-1};int dy[]={1,0,-1,0};bool go(int i,int j,int k,int z){ switch(s[z][i][j]){ case '+':return true; case '-':return k==0||k==2; case '|':return k==1||k==3; case '^':return k==3; case '>':return k==0; case '<':return k==2; case 'v':return k==1; case 'L':return k!=2; case 'R':return k!=0; case 'U':return k!=3; case 'D':return k!=1; case '*':return false; }}int cor(int k){ return k<=1?k+2:k-2;}bool check(int i,int j,int k,int z){ int x=i+dx[k],y=j+dy[k]; if(x<1||x>n||y<1||y>m||vis[z][x][y])return false; return go(i,j,k,z)&&go(x,y,cor(k),z);}void bfs(int x,int y){ queueqx,qy,q; qx.push(x); qy.push(y); q.push(0); vis[0][x][y]=1; while(!q.empty()){ x=qx.front(); y=qy.front(); int z=q.front(); qx.pop(); qy.pop(); q.pop(); for(int i=0;i<=3;i++) if(check(x,y,i,z)){ int xx=x+dx[i],yy=y+dy[i]; qx.push(xx); qy.push(yy); q.push(z); d[z][xx][yy]=d[z][x][y]+1; vis[z][xx][yy]=1; } if(!vis[(z+1)%4][x][y]){ d[(z+1)%4][x][y]=d[z][x][y]+1; vis[(z+1)%4][x][y]=1; qx.push(x); qy.push(y); q.push((z+1)%4); } }}char change(int i,int j,int k){ switch(s[k-1][i][j]){ case '+':return '+'; case '-':return '|'; case '|':return '-'; case '^':return '>'; case '>':return 'v'; case 'v':return '<'; case '<':return '^'; case 'L':return 'U'; case 'U':return 'R'; case 'R':return 'D'; case 'D':return 'L'; case '*':return '*'; }}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",s[0][i]+1); for(int k=1;k<=3;k++) for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) s[k][i][j]=change(i,j,k); int x,y; scanf("%d%d",&x,&y); bfs(x,y); int ans=inf; scanf("%d%d",&x,&y); for(int i=0;i<=3;i++) if(vis[i][x][y]) ans=min(ans,d[i][x][y]); if(ans==inf)ans=-1; printf("%d\n",ans); return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JProfiler11使用教程之JVM调优问题小结
下一篇:微信小程序平台,低功耗蓝牙的库,使用Promise封装,可快速且方便的进行蓝牙开发
相关文章

 发表评论

暂时没有评论,来抢沙发吧~