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.

InputThe 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.

OutputOutput 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 题意:一张n*m的地图,有一个男孩、一个女孩和两只鬼,每秒男孩走三步,女孩每秒一步,鬼每秒扩张2,也就是k秒后,所有与鬼的曼哈顿距离不大于2*k都会被鬼覆盖,求相遇最短时间。
思路:既然求最短时间,那么很容易想到bfs,对男孩bfs的同时对女孩bfs,当两者相遇的时候就是最短时间。
注:相当于鬼先走,然后人在走;另外注意模块化,我一开始写直接把男孩的三步用三重for去写,其实可以把男孩当成3次一步就好
#include<bits/stdc++.h>
using namespace std; char maps[][];
int ways[][] = {,,,,-,,,-};
int vis[][];
int n,m,t;
int ans;
struct Node
{
int x,y;
Node(int x=,int y=):x(x),y(y) {}
} ghost[],girl,boy; void init()
{
scanf("%d%d",&n,&m);
int cnt = ;
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
{
scanf("%s",maps[i]+);
for(int j=; j<=m; j++)
{
if(maps[i][j] == 'M')
boy = Node(i,j);
else if(maps[i][j] == 'G')
girl = Node(i,j);
else if(maps[i][j] == 'Z')
ghost[cnt++] = Node(i,j);
}
}
} bool check(int x,int y,int k)
{
if(x < || x > n || y < || y > m)
return ;
if(maps[x][y] == 'X')
return ;
if(abs(x - ghost[].x) + abs(y - ghost[].y) <= *k)
return ;
if(abs(x - ghost[].x) + abs(y - ghost[].y) <= *k)
return ;
return ;
}
queue<Node>que[];
bool bfs(int s)
{
int t = que[s].size();
while(t--)
{
Node tmp = que[s].front();
que[s].pop();
if(!check(tmp.x,tmp.y,ans))
continue;
for(int i=; i<; i++)
{
int xx = tmp.x + ways[i][];
int yy = tmp.y + ways[i][];
if(check(xx,yy,ans))
{
if(vis[xx][yy] && (vis[xx][yy]&) != ((s+)&))
return ;
else if(!vis[xx][yy])que[s].push(Node(xx,yy)),vis[xx][yy] = s+;
}
}
}
return ;
} int cal()
{
memset(vis,,sizeof(vis));
while(!que[].empty())que[].pop();
while(!que[].empty())que[].pop();
que[].push(boy);
que[].push(girl);
vis[boy.x][boy.y] = ;
vis[girl.x][girl.y] = ;
ans = ;
while(!que[].empty() || !que[].empty())
{
ans++;
if(bfs())
return ans;
if(bfs())
return ans;
if(bfs())
return ans;
if(bfs())
return ans;
}
return -;
} int main()
{
scanf("%d",&t);
while(t--)
{
init();
printf("%d\n",cal());
}
}

Nightmare Ⅱ HDU - 3085 (双向bfs)的更多相关文章

  1. HDU - 3085 双向BFS + 技巧处理 [kuangbin带你飞]专题二

    题意:有两只鬼,一个男孩女孩被困在迷宫中,男孩每秒可以走三步,女孩只能1步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...

  2. hdu 3085(双向bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 思路:双向广搜,每次从M出发,搜三步,从G出发,搜一步,然后就是判断是否走到对方已经走过的格子, ...

  3. Eight HDU - 1043 (双向BFS)

    记得上人工智能课的时候老师讲过一个A*算法,计算估价函数(f[n]=h[n]+g[n])什么的,感觉不是很好理解,百度上好多都是用逆向BFS写的,我理解的逆向BFS应该是从终点状态出发,然后把每一种状 ...

  4. kuangbin专题 专题二 搜索进阶 Nightmare Ⅱ HDU - 3085

    题目链接:https://vjudge.net/problem/HDU-3085 题意:有两个鬼和两个人和墙,鬼先走,人再走,鬼每走过的地方都会复制一个新鬼, 但新鬼只能等待旧鬼走完一次行程之后,下一 ...

  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. HDU 3085 Nightmare Ⅱ (双向BFS)

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

  8. HDU 3085 Nightmare Ⅱ 双向BFS

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

  9. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

随机推荐

  1. 【转载】 Eclipse注释模板设置详解

     Eclipse注释模板设置详解 网站推荐: 金丝燕网(主要内容是 Java 相关) 木秀林网(主要内容是消息队列)

  2. Django框架之Form组件

    一.初探Form组件 在介绍Form组件之前,让大家先看看它强大的功能吧!Go... 下面我们来看看代码吧! 1.创建Form类 from django.forms import Form from ...

  3. 《剑指offer》从上往下打印二叉树

    本题来自<剑指offer> 从上往下打印二叉树 题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 队列的思想. 先将根节点加入,当取该节点时候,依次将左右子树加入,直 ...

  4. 《剑指offer》重建二叉树

    本题来自<剑指offer> 重构二叉树 题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2 ...

  5. swagger2访问url

    swagger : http://localhost:8080/swagger/index.html springboot中的swagger:http://localhost:8080/swagger ...

  6. Java 9 中的 9 个新特性你知道吗

    摘要: Java 8 发布三年多之后,即将快到2017年7月下一个版本发布的日期了. 你可能已经听说过 Java 9 的模块系统,但是这个新版本还有许多其它的更新. 这里有九个令人兴奋的新功能将与 J ...

  7. MyEclipes相关配置

    0. MyEclipes10 相关下载资源(私人珍藏版) 链接:http://pan.baidu.com/s/1eSIdObS密码:0cjy 1. myEclipes连接Tomcat http://w ...

  8. 使用CSS选择器定位页面元素

    摘录:http://blog.csdn.net/defectfinder/article/details/51734690 CSS选择器也是一个非常好用的定位元素的方法,甚至比Xpath强大.在自动化 ...

  9. VUE失去焦点提交修改值

    xxx.vue <input class="ml6 w85 bdr-6 bd-none text-center" type="text" v-model= ...

  10. C# float与UInt16互转

    //float拆分成两个UInt16 public static UInt16 FloatToTwoUInt16(float f) { byte[] bs = BitConvert.GetBytes( ...