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.

题意:有一个n*m的地图,每个点代表一个房间,每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等。现在给出起点和终点,每次你可以把全部房间旋转90度或者移动到相邻的房间,但前提是两个房间之间都有有门,现在要你求起点出发到终点的最少时间。

思路:用vis[x][y][state]来标记当前元素已在队列当中,state表示当前全部房间旋转了多少次。head为当前位置,next为下一步的可以到达的位置,g[head.u][head.v] & ((1<<(i+next.state)%4))(判断当前的房间是否有门)&& g[next.u][next.v] & ((1<<(i+next.state+2)%4))(判断相邻的房间是否有门), i代表当前准备走的方向,g[x][y]代表每个方向是否可以走的十进制数值,例如0001代表左边有门,每位分别代表上右下左方向是否有门,1代表有,0代表没,最先到达终点的即为最小操作数;

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;  #define maxn int(1e3+20)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define PI acos(-1.0)
typedef long long ll;
char c;
int g[maxn][maxn],n,m;
struct Point
{int u,v,time,state;Point(){time = 0;};Point(int _u, int _v, int _time, int _state):u(_u),v(_v),time(_time),state(_state){}
}s,t;
int fun(char x)
{switch(x){case '^': return 8;break;case 'v': return 2;break;case '<': return 1;break;case '>': return 4;break;case '+': return 15;break;case '-': return 5;break;case '|': return 10;break;case 'L': return 14;break;case 'R': return 11;break;case 'U': return 7;break;case 'D': return 13;break;case '*': return 0;break;}
}
int dir[4][2] ={{0,-1},{1,0},{0,1},{-1,0}};
bool vis[maxn][maxn][5];
int bfs()
{memset(vis,0,sizeof(vis));queue<Point>q;Point next,head;q.push(s);vis[s.u][s.v][s.state] = 1;while(!q.empty()){head = q.front(); q.pop();if(head.u == t.u && head.v == t.v){return head.time;}if(!vis[head.u][head.v][(head.state+1)%4]){q.push(Point(head.u, head.v,head.time +1,(head.state+1)%4));vis[head.u][head.v][(head.state+1)%4] = 1;}for(int i = 0; i<4; i++){next.u = head.u + dir[i][0];next.v = head.v + dir[i][1];if(g[next.u][next.v] != 0){next.state = head.state;if(next.u >= 1 && next.u <= n && next.v >= 1 && next.v<= m && g[head.u][head.v] & ((1<<(i+next.state)%4))&& g[next.u][next.v] & ((1<<(i+next.state+2)%4))){    if(!vis[next.u][next.v][next.state]){next.time = head.time + 1;q.push(next);vis[next.u][next.v][next.state] = 1;}}}}}return -1;
}
int main()
{
#ifdef CDZSC_June  freopen("t.txt", "r", stdin);
#endif while(~scanf("%d%d",&n,&m)){for(int i = 1; i<=n; i++){for(int j = 1; j<=m; j++){scanf(" %c",&c);g[i][j] = fun(c);}}scanf("%d%d",&s.u,&s.v);scanf("%d%d",&t.u,&t.v);s.state = t.state = 0;printf("%d\n",bfs());}return 0;
}  

Codeforces 676D Theseus and labyrinth 模拟+bfs相关推荐

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

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

  2. 【CodeForces - 616C】The Labyrinth(bfs,并查集,STLset)

    题干: 求每个*能够到达的格子数量,只有.可以走(四个方向扩展),结果mod 10,替换 * 后输出. Input The first line contains two integers n, m  ...

  3. poj1426_模拟BFS

    题意:给出一个200以内的数n,求出这个数的倍数M,使得M中只有0和1组成.M最多100位. 分析:这个题竟然用的是bfs的思想.不看讨论真的想不出来.思路是这样的: 1.最高位一定是1.curnum ...

  4. CodeForces 1463 C. Busy Robot 模拟

    CodeForces 1463 C. Busy Robot 模拟 题目大意: 有一个一维坐标轴,在最初时刻有个机器人位于坐标 0 0 0 位置,有 n n n 个命令,对于每一个命令在 t i t_i ...

  5. 【CodeForces Round #550】A-F | 模拟 | 贪心 | 高精 | BFS | 二分图 | E

    今年怎么没有愚人节比赛了   CF你看看人家洛谷   唉鸭原来那边还没到愚人节呢- 愚人节比赛还是有的,在今晚 qwq [CodeForces 1144   A-F] Tags:模拟 贪心 BFS 高 ...

  6. 【25.93%】【676D】Theseus and labyrinth

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  8. csu 1536 Bit String Reordering(模拟 bfs+状态压缩)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1536 题意: 输入n个只为 0或1 的数 形成一个排列 再输入m个数 每个数代表 目标排列 (样例 ...

  9. CodeForces - [ACM-ICPC Jiaozuo Onsite D]Honeycomb(BFS)

    题目链接:https://codeforces.com/gym/102028/problem/F Time limit: 4.0 s Memory limit: 1024 MB Problem Des ...

最新文章

  1. 如何在 Linux 中创建一个共享目录
  2. 哪个是更早的时间 date_淘宝用户行为分析(漏斗模型+pv,uv,pv/uv,复购率,用户活跃时间段)...
  3. win7 64位安装redis 及Redis Desktop Manager使用
  4. SQL Server 2005登录名,用户名,角色,架构之间的关系
  5. 120. Triangle
  6. springCloud - 第6篇 - 网关的实现:ZUUL
  7. SQLSERVER 执行过的语句查询
  8. 如何在eclipse里使用git
  9. Map转成JSON对象
  10. 《概率论与数理统计》学习笔记
  11. 招商银行一网通支付(php接入招商银行一网通支付)
  12. 数组最大值/最小值计算方法
  13. 简单excel饼状图怎么做,bi工具怎么做饼状图
  14. 分布式任务调度平台XXL-JOB深度实战
  15. [日常训练] Surprise me
  16. 计算机主板测评,9款主板特色设计对比
  17. 【NOIP2018】摆渡车
  18. Excel如何将引用的sheet名称全部替换。
  19. 计算机不能访问网络录像机,电脑摄像头打不开不能进行视频对话怎么解决
  20. 解决Ranorex在测试执行过程中,当执行完调用外界库的方法后并没有执行其他的操作?

热门文章

  1. 对于建站程序 织梦、帝国、wordpress 哪个好?
  2. Java实验3 第十一题:游戏:双骰儿赌博
  3. 计算机技术论文搜索引擎,搜索引擎-毕设论文.doc
  4. crc-itu java实现_JAVA编程心得-JAVA实现CRC-CCITT(XMODEM)算法
  5. 计算机软件著作权登记分类号如何选择?
  6. 景联文科技出席全国信标委生物特征识别委会2021年下半年工作组集中会议
  7. kube-proxy ipvs模式详解
  8. 关闭X-Powered-By 信息(隐藏PHP版本信息)
  9. 脚本链接 ssh 自动输入密码
  10. python怎么求圆柱表面积半径和高由键盘输入_Java圆柱体表面积和体积计算代码实例...