Codeforces Round #354 (Div. 2)-Theseus and labyrint

原文链接

题中给的迷宫有四个状态,我把每个状态对应的图都生成。用宽搜求出从原点到每个状态的迷宫的每个格子的最小距离.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue> 
#include <cmath>
#include <algorithm>
#define maxn 1005 
#define INF 1e9
typedef long long ll;
using namespace std;

struct Node{
	Node(int a, int b, int c){
		x = a;
		y = b;
		p = c;
	} 
	int x, y;
	int p;
};
char vis[4][maxn][maxn];
int d[4][maxn][maxn], xt, yt, xm, ym, n, m;
char tra[] = {"++-|-^>v<^LURDL**"};
bool dir[200][4];
int kk[4][2] = {-1, 0, 0, 1, 0, -1, 1, 0};
void Init(){
	
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++)
	  scanf("%s", vis[0][i]);
	scanf("%d%d%d%d", &xt, &yt, &xm, &ym);
	xt--;
	yt--;
	xm--;
	ym--;
	memset(d, -1, sizeof(d));
	dir['+'][0] = dir['+'][1] = dir['+'][2] = dir['+'][3] = true;
	dir['-'][2] = dir['-'][1] = true;
	dir['|'][0] = dir['|'][3] = true;
	dir['^'][0] = true;
	dir['>'][1] = true;
	dir['v'][3] = true;
	dir['<'][2] = true;
	dir['L'][0] = dir['L'][1] = dir['L'][3] = true;
	dir['U'][1] = dir['U'][2] = dir['U'][3] = true;
	dir['R'][2] = dir['R'][0] = dir['R'][3] = true;
	dir['D'][0] = dir['D'][1] = dir['D'][2] = true;
	for(int i = 1; i < 4; i++){
		for(int h1 = 0; h1 < n; h1++)
		  for(int h2 = 0; h2 < m; h2++){
		  	for(int h3 = 0; tra[h3]; h3++){
		  		if(tra[h3] == vis[i-1][h1][h2]){
		  		vis[i][h1][h2] = tra[h3+1];
		  		 break;
		  	  }
		  	}
		  }
	}
}  
bool judge(int x, int y, Node &p, int k){
	
	if(x >= 0 && x < n && y >= 0 && y < m && d[p.p][x][y] == -1){
		char ch1 = vis[p.p][x][y];
		char ch2 = vis[p.p][p.x][p.y];
		if(dir[ch2][k] && dir[ch1][3-k])
		  return true;
	}
	return false;
}
int bfs(){
	
	Node p = Node(xt, yt, 0);
	queue<Node> q;
	q.push(p);
	d[0][xt][yt] = 0;
	while(!q.empty()){
		
		p = q.front();
		q.pop();
		for(int i = 0; i < 4; i++){
			int x = p.x + kk[i][0];
			int y = p.y + kk[i][1];
			if(judge(x, y, p, i)){
				d[p.p][x][y] = d[p.p][p.x][p.y] + 1;
				if(x == xm && y == ym)
				 return d[p.p][x][y];
				q.push(Node(x, y, p.p));
			}
		}
		int c = p.p;
		c = (c + 1) % 4;
		if(d[c][p.x][p.y] == -1){
			d[c][p.x][p.y] = d[p.p][p.x][p.y] + 1;
			q.push(Node(p.x, p.y, c));
		}
	}
	return -1;
}
int main(){
	
	//freopen("in.txt", "r", stdin);
	Init();
	if(xt == xm && yt == ym)
	 printf("%d\n", 0);
	else{
		printf("%d\n", bfs());
	}
	return 0;
}

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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值