【Codeforces Round 354 (Div 2)D】【迷宫搜索BFS】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) 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.




#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1010, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, m;
char s[N][N];
int sty, stx, edy, edx;
int dy[4] = {-1,0,1,0};
int dx[4] = {0,1,0,-1};
bool ok[N][N][4][4];
bool vis[N][N][4];
void init()
{
	MS(vis, 0);
	MS(ok, 0);
	for (int i = 1; i <= n; ++i)
	{
		scanf("%s", s[i] + 1);
		for (int j = 1; j <= m; ++j)
		{
			if (s[i][j] == '+')
			{
				for (int k = 0; k < 4; ++k)ok[i][j][0][k] = 1;
			}
			else if (s[i][j] == '|')
			{
				ok[i][j][0][0] = 1;
				ok[i][j][0][2] = 1;
			}
			else if (s[i][j] == '-')
			{
				ok[i][j][0][1] = 1;
				ok[i][j][0][3] = 1;
			}
			else if (s[i][j] == '^')
			{
				ok[i][j][0][0] = 1;
			}
			else if (s[i][j] == '>')
			{
				ok[i][j][0][1] = 1;
			}
			else if (s[i][j] == 'v')
			{
				ok[i][j][0][2] = 1;
			}
			else if (s[i][j] == '<')
			{
				ok[i][j][0][3] = 1;
			}
			else if (s[i][j] != '*')
			{
				for (int k = 0; k < 4; ++k)ok[i][j][0][k] = 1;
				if (s[i][j] == 'U')ok[i][j][0][0] = 0;
				if (s[i][j] == 'R')ok[i][j][0][1] = 0;
				if (s[i][j] == 'D')ok[i][j][0][2] = 0;
				if (s[i][j] == 'L')ok[i][j][0][3] = 0;
			}
			for (int dep = 0; dep < 3; ++dep)
			{
				for (int k = 0; k < 4; ++k)
				{
					ok[i][j][dep + 1][k + 1 & 3] = ok[i][j][dep][k];
				}
			}
		}
	}
	scanf("%d%d", &sty, &stx);
	scanf("%d%d", &edy, &edx);
}
struct node
{
	int y, x, dep, step;
};
node q[(int)4e6 + 10]; int h, t;
int bfs()
{
	h = t = 0;
	q[t++] = { sty,stx,0,0 };
	while (h < t)
	{
		int y = q[h].y;
		int x = q[h].x;
		int dep = q[h].dep;
		int step = q[h++].step;
		if (y == edy&&x == edx)return step;
		for (int i = 0; i < 4; ++i)if(ok[y][x][dep][i])
		{
			int yy = y + dy[i];
			int xx = x + dx[i];
			if (yy<1 || yy>n || xx<1 || xx>m)continue;
			if (!vis[yy][xx][dep] && ok[yy][xx][dep][i + 2 & 3])
			{
				vis[yy][xx][dep] = 1;
				q[t++] = { yy,xx,dep,step + 1 };
			}
		}
		if (!vis[y][x][dep + 1 & 3])
		{
			vis[y][x][dep + 1 & 3] = 1;
			q[t++] = { y,x,dep + 1 & 3,step + 1 };
		}
	}
	return -1;
}
int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		init();
		printf("%d\n", bfs());
	}
	return 0;
}
/*
【题意】
有一个n(1000)*m(1000)的迷宫,
每个格子有4个方向,然后一些方向上有门。
我们可以选择用1时间的单位成本,使得每一个格子都在自己所在的位置顺时针旋转90度
也可以选择用1时间的单位成本,使得走到相邻的格子中去,前提是两个格子相对应的位置都有门。
问你从st点走到ed点的最小时间

【类型】
BFS

【分析】
旋转90度,最多只有4种不同的地图。
于是我们把地图拆成4份,预处理出每一份地图上的连通性,然后暴力bfs即可。

【时间复杂度&&优化】
O(4nm)

*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值