Nightmare Ⅱ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3012    Accepted Submission(s): 856

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.
 

Sample Input

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
 
Sample Output

1
1
-1

思路:

双向BFS,只要其中一个人走到了另一个人走过的地方就算相遇。

判断鬼这里用到了曼哈顿距离,就是鬼可以每步向外扩张两格范围,所以只要在每次走完看一下有没有和鬼超过2*step就行了(这里要注意鬼先走)。还有每次走之前要判断走之前的点有没有被鬼占领,一直wa在这里找不到哪里错了。

Code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
//#include<map>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
const int N=810;
using namespace std;
struct node{
int x,y;
};
int step,n,m,mx,my,gx,gy,zx[2],zy[2],to[4][2]={0,1,0,-1,1,0,-1,0};
char map[N][N];
int vis[2][N][N];
queue<node> q[2];
int bfs(int x){
node a,b;
int Count=q[x].size();
while(Count--){
a=q[x].front();
q[x].pop(); /**这里要注意走之前一定要先对a进行判断**/
if((abs(a.x-zx[0])+abs(a.y-zy[0]))<=2*step) continue; //曼哈顿距离
if((abs(a.x-zx[1])+abs(a.y-zy[1]))<=2*step) continue; for(int i=0;i<4;i++){
b.x=a.x+to[i][0];
b.y=a.y+to[i][1];
if(b.x<0 || b.y<0 || b.x>=n || b.y>=m) continue;
if(map[b.x][b.y]=='X') continue;
if(vis[x][b.x][b.y]) continue;
if((abs(b.x-zx[0])+abs(b.y-zy[0]))<=2*step) continue; //曼哈顿距离
if((abs(b.x-zx[1])+abs(b.y-zy[1]))<=2*step) continue;
vis[x][b.x][b.y]=1;
if(vis[0][b.x][b.y] && vis[1][b.x][b.y]) return 1;
q[x].push(b);
}
}
return 0;
}
int solve(){
node a;
memset(vis,0,sizeof(vis));
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
vis[0][mx][my]=1;
vis[1][gx][gy]=1;
a.x=mx;a.y=my;
q[0].push(a);
a.x=gx;a.y=gy;
q[1].push(a);
step=0;
while((!q[0].empty()) || (!q[1].empty())){
step++;
if(bfs(0)) return step;
if(bfs(0)) return step;
if(bfs(0)) return step;
if(bfs(1)) return step;
}
return -1;
}
int main(){
int c,k;
scanf("%d",&c);
while(c--){
scanf("%d%d",&n,&m);
k=0;
for(int i=0;i<n;i++){
scanf("%s",map[i]);
for(int j=0;j<m;j++){
if(map[i][j]=='M'){
mx=i;my=j;
}
else if(map[i][j]=='G'){
gx=i;gy=j;
}
else if(map[i][j]=='Z'){
zx[k]=i;zy[k++]=j;
}
}
}
int ans=solve();
printf("%d\n",ans);
}
return 0;
}

HDU3085(双向BFS+曼哈顿距离)题解的更多相关文章

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

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

  2. HDU 6171 Admiral(双向BFS+队列)题解

    思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...

  3. 小乐乐打游戏(BFS+曼哈顿距离)

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 小乐乐觉得学习太简单了,剩下那么多的时间好无聊 ...

  4. [题解](双向bfs)hdu_3085_Nightmare Ⅱ

    发现直接搜索比较麻烦,但是要同时两个人一起走容易想到双向bfs,比较普通, 在判断是否碰到ghost时只要比较两点的曼哈顿距离大小和step*2(即ghost扩散的距离)即可,仔细思考也是可以想到的 ...

  5. HDU3085NightmareII题解--双向BFS

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3085 分析 大意就是一个男孩和一个女孩在网格里,同时还有两个鬼,男孩每轮走三步,女孩每轮走一步,与鬼曼 ...

  6. 【HDU3085】nightmare2 双向BFS

    对于搜索树分支很多且有明确起点和终点的情况时,可以采用双向搜索来减小搜索树的大小. 对于双向BFS来说,与单向最大的不同是双向BFS需要按层扩展,表示可能到达的区域.而单向BFS则是按照单个节点进行扩 ...

  7. Codeforces 1093G题解(线段树维护k维空间最大曼哈顿距离)

    题意是,给出n个k维空间下的点,然后q次操作,每次操作要么修改其中一个点的坐标,要么查询下标为[l,r]区间中所有点中两点的最大曼哈顿距离. 思路:参考blog:https://blog.csdn.n ...

  8. HDU3085 Nightmare Ⅱ (双向BFS)

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

  9. HDU 3085 Nightmare Ⅱ 双向BFS

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

随机推荐

  1. LightOJ 1038 - Race to 1 Again(期望+DP)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1038 题意是:给你一个N (1 ≤ N ≤ 105) 每次N都随机选一个因子d,然后让 ...

  2. Shell 和Python的区别。

    shell 应该属于宏语言,顾名思义是系统的壳,方便与系统交互的在以下情况下,不使用shell,因为shell对此无能为力:如:跨平台,较复杂数学操作(如浮点运算,精确运算等),图形化界面 GUI,I ...

  3. 如何控制dedecms描述的长度?

    我们都知道调用dedecms的标题长度可以用titlelen='字符数',{dede:arclist titlelen='10'},表示标题长度为10个字符,也即是5个汉字.如果想要控制描述的调用长度 ...

  4. 梯度下降法实现-python[转载]

    转自:https://www.jianshu.com/p/c7e642877b0e 梯度下降法,思想及代码解读. import numpy as np # Size of the points dat ...

  5. $scope.$apply

    对于一个在前端属于纯新手的我来说,Javascript都还是一知半解,要想直接上手angular JS,遇到的阻力还真是不少.不过我相信,只要下功夫,即使是反人类的设计也不是什么大的问题. Okay, ...

  6. c++多个文件中如何共用一个全局变量

    例子: 头文件:state.h   源文件:state.cpp 其它源文件:t1.cpp  t2.cpp  t3.cpp, 这些源文件都包含头文件state.h. 需要定义一个全局变量供这些源文件中使 ...

  7. 删除排序数组中的重复数字 II

    题目连接 http://www.lintcode.com/zh-cn/problem/remove-duplicates-from-sorted-array-ii/ 题目大意 跟进“删除重复数字”: ...

  8. c#null值加法运算

    加号都是一个含义啊,操作数不同,加号重载的方法就不一样,当加号的左边或右边含有字符串的时候,总是返回一个不为空的字符串.当加号左右两边都是数值的时候,就会对其进行数学运算,null+任何数都为null ...

  9. VS2010/MFC编程入门之四十五(MFC常用类:CFile文件操作类)

    上一节中鸡啄米讲了定时器Timer的用法,本节介绍下文件操作类CFile类的使用. CFile类概述 如果你学过C语言,应该知道文件操作使用的是文件指针,通过文件指针实现对它指向的文件的各种操作.这些 ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON SmallestRectangle1

    zw版[转发·台湾nvp系列Delphi例程]HALCON SmallestRectangle1 procedure TForm1.Button1Click(Sender: TObject);var ...