当前位置 : 主页 > 编程语言 > java >

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

来源:互联网 收集:自由互联 发布时间:2022-08-15
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 th


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
+*
*U
1 1
2 2

output

-1

input

2 3
<><
><>
1 1
2 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代码:

#include<bits/stdc++.h>
using 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){
queue<int>qx,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;
}





上一篇:Codeforces Round #354 (Div. 2) E (数学题)
下一篇:没有了
网友评论