codeforces 676D (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.


题意:一个人从入口走到出口的最短时间.给出一堆格子的类型分别表示这个格子能

通向哪个格子,两个格子ab互通当且仅当a能通向b并且b能通向a.还可以花费1单位

时间把地图上的所有格子都顺时针旋转90度.求出最短时间.

有个坑就是可能起点终点是同一个格子.

因为地图最多的情况就是四种直接把这四种都存下来.然后直接从原点出发bfs,

每次要判断两个格子能不能互相到达可以分开成四种函数来写.

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
using namespace std;
#define maxn 1111

int n, m;
char mp[4][maxn][maxn];

bool legal (int x, int y) {
	if (x < 0 || y < 0 || x >= n || y >= m)
		return 0;
	return 1;
}

#define move Move
const int move[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};//上下左右
int x, xx, y, yy;
int vis[maxn][maxn][5];
int change[maxn*maxn];

struct node {
	int x, y, mod, num;
};
queue <node> gg;

bool ok_d (char a) {
	if (a == '+' || a == '|' || a == 'v' || a == 'U' || a == 'L' || a == 'R')
		return 1;
	return 0;
}
bool ok_u (char a) {
	if (a == '+' || a == '|' || a == '^' || a == 'D' || a == 'L' || a == 'R')
		return 1;
	return 0;
}
bool ok_l (char a) {
	if (a == '+' || a == '-' || a == '<' || a == 'R' || a == 'D' || a == 'U')
		return 1;
	return 0;
}
bool ok_r (char a) {
	if (a == '+' || a == '-' || a == '>' || a == 'U' || a == 'D' || a == 'L')
		return 1;
	return 0;
}

bool ok (node now, node next, int t, int op) { 
	char a = mp[t][now.x][now.y], b = mp[t][next.x][next.y];
	if (op == 0) {
		if (ok_u (a) && ok_d (b))
			return 1;
		else 
			return 0;
	}
	else if (op == 1) {
		if (ok_d (a) && ok_u (b))
			return 1;
		else 
			return 0;
	}
	else if (op == 2) {
		if (ok_l (a) && ok_r (b)) 
			return 1;
		else 
			return 0;
	}
	else if (op == 3) {
		if (ok_r (a) && ok_l (b))
			return 1;
		else 
			return 0;
	}
	return 0;
}

bool bfs () {
	while (!gg.empty ())
		gg.pop ();
	memset (vis, 0, sizeof vis);
	gg.push ((node) {x, y, 0, 0});
	vis[x][y][0] = 0;
	while (!gg.empty ()) {
		node now = gg.front (); gg.pop ();
		if (now.x == xx && now.y == yy) {
			cout << now.num << "\n";
			return 1;
		}
		if (!vis[now.x][now.y][(now.mod+1)%4]) {
			gg.push ((node) {now.x, now.y, (now.mod+1)%4, now.num+1});
			vis[now.x][now.y][(now.mod+1)%4] = 1;
		}
		for (int i = 0; i < 4; i++) {
			node next = now;
			next.num = now.num+1;
			next.x += move[i][0], next.y += move[i][1];
			if (vis[next.x][next.y][next.mod] || !legal (next.x, next.y) || !ok (now, next, now.mod, i)) 
				continue;
			gg.push (next);
			vis[next.x][next.y][next.mod] = 1;
 		}
	}
	return 0;
}

int main () {
	change['+'] = '+';
	change['-'] = '|'; 
	change['|'] = '-';
	change['^'] = '>';
	change['v'] = '<';
	change['>'] = 'v';
	change['<'] = '^';
	change['L'] = 'U';
	change['R'] = 'D';
	change['U'] = 'R';
	change['D'] = 'L';
	change['*'] = '*';
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> mp[0][i];
	for (int t = 1; t < 4; t++) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				mp[t][i][j] = change[mp[t-1][i][j]];
			}
		}
	}
	cin >> x >> y >> xx >> yy;
	x--, y--, xx--, yy--;
	if (x == xx && y == yy) {
		printf ("0\n");
		return 0;
	}
	if (!bfs ()) {
		cout << "-1" << "\n";
	}
	return 0;
}



1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值