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

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) and wants to know the minimum number of minutes required to get there.

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 characters, describing the blocks of the labyrinth. The possible characters are:

  • «+» 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.

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 from left to right.

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

Last line contains two integers — coordinates of the block (xM, yM) (1 ≤ xM ≤ n1 ≤ 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 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the block where Minotaur is hiding.

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;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值