Problem Description

Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.

Note: the new ghosts also can devide as the original ghost.

Input

The input starts with an integer T, means the number of test cases.

Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)

The next n lines describe the maze. Each line contains m characters. The characters may be:

‘.’ denotes an empty place, all can walk on.

‘X’ denotes a wall, only people can’t walk on.

‘M’ denotes little erriyue

‘G’ denotes the girl friend.

‘Z’ denotes the ghosts.

It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.

Output

Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.

SampleInput

3
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G... 10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X

SampleOutput

1
1
-1 题意就是给你一个迷宫,不对,就是说你现在被困在迷宫里,要去和你GF见面,但是迷宫中还有人不可以走的墙和会杀死人的幽灵,幽灵每个单位时间都会往上下左右延申新的幽灵。
幽灵有两个,M是你的位置,G是GF的位置。
幽灵每秒可以延申两格,你可以每秒走三步,GF每秒只能走一步,如果在不被杀死的情况和女朋友汇合就输出最小单位时间,否则输出-1.
(注意幽灵是可以往墙上延申的)
因为两个人都可以动,不用说,双向BFS是肯定的,不过有一点就是,如何实现每秒走三步以及判断是否被杀死呢?
答案是我也不知道。这道题目就留给各位自己思考。
=7=嘻嘻 不闹了。其实可以换个思路去思考,我们可以用三个bfs代替走三步,但是如何判断是否被杀死呢,其实这道题可以不需要判断是否被杀死,而是判断人物走到能否在幽灵延申到某个位置之前走到那个位置,用曼哈顿距离判断就行了。 提一下什么是曼哈顿距离
常用距离度量方法有十一种,而我们大部分时间只用到欧氏距离和曼哈顿距离。
设两个点的坐标(X1,Y1),(X2,Y2);
欧氏距离就是坐标的直线距离 = sqrt((X2 - X1)2+(Y2 - Y1)2)
而曼哈顿距离就是以欧式距离为斜边构造直角三角形的两直角边和 = |X2 - X1| + |Y2 - Y1|
为什么有这么多构造方式以及其区别,这篇博文就不详细介绍了。 值得一题的是,因为是双向搜索,所以需要开两个二维数组或是一个三维数组分别标记你或者GF是否走过。
还有就是代码BFS中的
 int len = q[w].size();
while(len--)

因为我们不是一次就搜索完,我们的BFS仅仅只是做走一步的作用,所以只把当前已经存的点的下一点存入就行了。若是觉得难以理解可以替换成while(!q[w].empty)观察每一步的输出情况。

代码:
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int fx[][]={,,-,,,,,-};
char mp[][];
int vis[][][];
int gx,gy,mx,my,n,m,step;  //记录坐标,这里的step指的是GF走的步数,应该理解成走了多少单位时间
pair<int,int>cur,z[];  //z用来记录幽灵位置
queue<pair<int,int> >q[]; //分别记录你和GF的路径 bool check(pair<int,int> x){
if(x.F < || x.S < || x.F >= n || x.S >= m || mp[x.F][x.S] == 'X')
return false;
if((abs(x.F-z[].F)+abs(x.S-z[].S)) <= *step || (abs(x.F-z[].F)+abs(x.S-z[].S)) <= *step) //判断在幽灵延申到某个点之前是否能走到
return false;
return true;
} int bfs(int w){
pair<int,int>tp,next;
int len = q[w].size();
while(len--){  //注意这里不是搜完,因为是多次搜索,只需要把当前步骤行进完就行了
tp = q[w].front();
q[w].pop();
if(!check(tp)) continue;
for(int i = ; i < ; i++){
next.F = tp.F + fx[i][];
next.S = tp.S + fx[i][];
if(!check(next))
continue;
if(!vis[w][next.F][next.S]){
if(vis[-w][next.F][next.S]) //判断下一个点是否对方已经走过
return ;
vis[w][next.F][next.S] = ;
q[w].push(next);
}
}
}
return ;
} int solve(){
while(!q[].empty())
q[].pop();
while(!q[].empty())
q[].pop(); cur.F = mx;
cur.S = my;
q[].push(cur);
cur.F = gx;
cur.S = gy;
q[].push(cur);
MMT(vis);
vis[][mx][my] = vis[][gx][gy] = ;
step = ; while((!q[].empty()) || (!q[].empty())){
step++;
if(bfs()) //通过三次bfs达到走三步
return step;
if(bfs())
return step;
if(bfs())
return step;
if(bfs())
return step;
}
return -;
} int main(){
ios_base::sync_with_stdio(false);
cout.tie();
cin.tie();
int t;
cin>>t;
while(t--){
int cnt = ;
cin>>n>>m;
for(int i = ; i < n; i++)
cin>>mp[i];
for(int i = ; i < n; i++)
for(int j = ; j < m; j++){
if(mp[i][j] == 'G')
gx = i, gy = j;
if(mp[i][j] == 'M')
mx = i, my = j;
if(mp[i][j] == 'Z')
z[cnt].F = i, z[cnt++].S = j;
}
cout << solve() << endl;
}
return ;
}

Nightmare Ⅱ(双向BFS)的更多相关文章

  1. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  3. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  4. HDU3085 Nightmare Ⅱ (双向BFS)

    联赛前该练什么?DP,树型,状压当然是爆搜啦 双向BFS就是两个普通BFS通过一拼接函数联系,多多判断啦 #include <iostream> #include <cstdio&g ...

  5. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  6. HDU 3085 Nightmare Ⅱ(双向BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...

  7. HDU3085(双向BFS+曼哈顿距离)题解

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. BFS:HDU3085-Nightmare Ⅱ(双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

随机推荐

  1. 树莓派dht11,土壤湿度传感器,继电器的使用。树莓派云灌溉(二)

    关于传感器的一些说明 我的想法是这样的 我尽量用易于理解的语言去说我的想法 首先,土壤湿度传感器和dh11会获取数据,然后树莓派会处理这些数据,读出土壤温湿度和空气温湿度,并将这些数据上传到云服务器, ...

  2. 基于UDP的socket tcp和udp的区别(小白进击篇)

    目录 16.基于udp协议的socket通信 为什么udp不会有粘包现象 DGRAM datagram#数据报文 发送sento (发送的信息,发送给的地址) 接收revefrom 客户端 服务端 t ...

  3. hbase rowkey 设计

    HBase中的rowkey是按字典顺序排序的,通过rowkey查询可以对千万级的数据实现毫秒级响应.然而,如果rowkey设计不合理的话经常会出现一个很普遍的问题----热点.当大量client的请求 ...

  4. Linux云计算高端架构师+DevOps高级虚拟化高级进阶视频

    课程大纲 1.开班典礼(1)_rec.mp4 2.开班典礼(2)_rec.mp4 3.开班典礼(3)_rec.flv 4.Linux操作系统系统安装及启动流程(1)_rec.flv 5.Linux操作 ...

  5. 纯数据结构Java实现(3/11)(链表)

    题外话: 篇幅停了一下,特意去看看其他人写的类似的内容:然后发现类似博主喜欢画图,喜欢讲解原理. (于是我就在想了,理解数据结构的确需要画图,但我的文章写给懂得人看,只配少量图即可,省事儿) 下面正题 ...

  6. python + selenium webdriver 从主窗口A跳转至主窗口B后,无法定位窗口B的元素的问题

    在做登录脚本的时候,如果只是单纯从登录页面进行元素定位的话,并不存在这个问题 但实际情况是,从首页A进入到登录页面B(并非弹出框),这时候在页面B无法定位到该页面的元素 问题:从页面A进入页面B,无法 ...

  7. 打印机服务配置篇WindowsServer2008

    本次配置Server2008 打印服务器    目的实现Kingdee远程打印服务,直接在金蝶客户端部署打印机服务器 服务器角色: --打印服务器 --LPD服务 --Internet打印 *打印服务 ...

  8. 使用.Net Core + Vue + IdentityServer4 + Ocelot 实现一个简单的DEMO +源码

    运行环境 Vue 使用的是D2admin: https://doc.d2admin.fairyever.com/zh/ Github地址:https://github.com/Fengddd/Perm ...

  9. 一文搞懂Python中的所有数组数据类型

    关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...

  10. PL/SQL 监听程序当前无法识别连接描述符中请求的服务解决

    PL/SQL 用了几天后再登陆提示监听程序当前无法识别连接描述符中请求的服务,绞尽脑汁各种搜索找到以下解决方案-修改listener.ora文件 一般文件存在:app\Administrator\pr ...