原文链接

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

#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 ≤ 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 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.

Codeforces Round #354 (Div. 2)-Theseus and labyrint相关推荐

  1. Codeforces Round #354 (Div. 2) D. Theseus and labyrinth(bfs)

    题意:给出一个n*m的地图,英雄从(xt,yt)出发,要到达敌人所在地(xm,ym).地图每个格子有设定: ^>v<代表向箭头方向有门,其他方向没门: URDL代表某个方向没门,其他方向都 ...

  2. Codeforces Round #354 (Div. 2)-A

    A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...

  3. Codeforces Round #354 (Div. 2)

    贪心 A Nicholas and Permutation #include <bits/stdc++.h>typedef long long ll; const int N = 1e5 ...

  4. Codeforces Round #354 (Div. 2) A. Nicholas and Permutation

    Nicholas and Permutation time limit : 1 second memory limit: 256 megabytes 题目连接: http://www.codeforc ...

  5. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  6. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  7. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  8. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  9. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

最新文章

  1. php 打开动态链接,php调用com组件-dll文件(动态链接库)
  2. Spring 注解的作用
  3. 用 Flask 来写个轻博客 (32) — 使用 Flask-RESTful 来构建 RESTful API 之一
  4. python的socket编程
  5. iPhone开发入门守则:Objective-C编码规范--系列教程
  6. Spring Boot定制启动图案
  7. 阿里媒体转码公共参数_Xuggler教程:转码和媒体修改
  8. linux18.04循环登陆,ubuntu18.04 循环登陆
  9. 跨库事务处理 spring+hibernate+struts2+jta
  10. Java的一个关于“星球”的枚举
  11. 什么是3d建模,3D建模师可以从事哪些职业?
  12. 阿里立秋:淘宝如何做智能化UI测试?
  13. arcmap怎么保存相对路径_如何将arcgis的mxd文档存储为相对路径
  14. python求球的表面积_python中计算体积或表面积的好算法
  15. Android studio突然报错Entry name ‘META-INF/androidx.vectordrawable_vectordrawable.version‘ collided的解决办法
  16. linux rs,Linux中的RS, ORS, FS, OFS
  17. 虚拟机不正常关机,到时无法进入文本界面
  18. revit管线插件:当前楼层怎么显示楼板以下的给排水管道?
  19. 高等数学(下)重积分
  20. oracle密码锁了,Oracle 修改密码 解锁

热门文章

  1. 利用三级结构进行蛋白质嵌入的自我监督预训练
  2. 郎平卸任女排主帅后,将去北师大珠海校区工作!
  3. php 中文转拼音 开头大写(附中文转首字母大写/全拼小写)
  4. 服务器进入bios界面重装系统,电脑开机怎么进入bios界面(重装系统卡在首次使用)...
  5. android fragment相机,简单的易于集成的Android相机Fragment – Camer...
  6. 金山软件刘鑫:有限使用UML
  7. 上皮细胞膜纳米囊泡包裹药物如紫杉醇,喜树碱,阿霉素
  8. 安装Microsoft.UI.Xaml.2.6(WSA安卓子系统安装缺失)
  9. 中国汽车流通协会:2018年7月二手车市场分析
  10. scrapy爬取当当网