Nightmare Ⅱ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2196    Accepted Submission(s): 572

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
 
被读入卡了超时,按行读快,一个一个读超时
 //2017-03-08
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; struct node
{
int x, y;
void setNode(int x, int y)
{
this->x = x;
this->y = y;
}
};
int n, m, zx[], zy[], ans, TIME;
char grid[][];
bool vis[][][], ok;
int dx[] = {, , , -};
int dy[] = {, -, , };
queue<node> q[]; bool judge(int x, int y)
{
if(grid[x][y] == 'X')return false;
for(int i = ; i < ; i++)
if((abs(x-zx[i])+abs(y-zy[i]))<=*TIME)
return false;
return true;
} bool bfs(int id)
{
int x, y, nx, ny, s;
node tmp;
s = q[id].size();
while(s--)
{
x = q[id].front().x;
y = q[id].front().y;
q[id].pop();
if(!judge(x, y))continue;
for(int i = ; i < ; i++)
{
nx = x + dx[i];
ny = y + dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&judge(nx, ny)&&!vis[id][nx][ny])
{
if(vis[id^][nx][ny]){
ok = true;
return true;
}
vis[id][nx][ny] = ;
tmp.setNode(nx, ny);
q[id].push(tmp);
}
}
}
return false;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
node tmp;
while(!q[].empty())q[].pop();
while(!q[].empty())q[].pop();
memset(vis, , sizeof(vis));
scanf("%d%d", &n, &m);
getchar();
for(int i = ; i < n; i++)//被读入卡了超时,按行读快,一个一个读超时
scanf("%s", grid[i]);
int cnt = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < m; j++)
{
if(grid[i][j] == 'Z'){
zx[cnt] = i;
zy[cnt] = j;
cnt++;
}
if(grid[i][j] == 'M'){
tmp.setNode(i, j);
vis[][i][j] = ;
q[].push(tmp);
}
if(grid[i][j] == 'G'){
tmp.setNode(i, j);
vis[][i][j] = ;
q[].push(tmp);
}
}
}
ok = false;
TIME = ;
while(!q[].empty() || !q[].empty())
{
TIME++;
if(bfs())break;
if(bfs())break;
if(bfs())break;
if(bfs())break;
}
if(ok)printf("%d\n", TIME);
else printf("-1\n");
}
return ;
}

HDU3085(KB2-G 双向bfs)的更多相关文章

  1. 【HDU3085】nightmare2 双向BFS

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

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

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

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

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

  4. HDU3085 Nightmare Ⅱ (双向BFS)

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

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

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

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

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

  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. BFS、双向BFS和A*

    BFS.双向BFS和A* Table of Contents 1. BFS 2. 双向BFS 3. A*算法 光说不练是无用的.我们从广为人知的POJ 2243这道题谈起:题目大意:给定一个起点和一个 ...

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

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

随机推荐

  1. Apache Log4j配置说明

    1.Log4j简介 Log4j是Apache的一个开源项目,它允许开发者以任意间隔输出日志信息.Log4j主要由三大类组件构成: 1)Logger-负责输出日志信息,并能够对日志信息进行分类筛选,即决 ...

  2. NSTimer、performSelector 函数没有被调用的原因

    performSelector 指定的方法没有被调用 Invokes a method of the receiver on the current thread using the default ...

  3. python学习笔记04-格式化输出

    使用占位符来进行格式化输出 %S %d %f Exit()  程序退出函数

  4. POJ 1102

    #include<iostream>// cheng da cai zi 11.14 using namespace std; int main() { int i; int j; int ...

  5. 简述ARP请求过程(同一子网和不同子网)

    1. 同一子网 主机A在准备构造链路层以太网帧头时,首先根据目的IP去查找ARP表,如果找到对应项,则直接得到目的MAC地址,如果没有查到才执行上面所说的ARP广播请求.这样做是为了最大限度地减少广播 ...

  6. (转)飘逸的python - 增强的格式化字符串format函数

    原文:https://blog.csdn.net/handsomekang/article/details/9183303 Python字符串格式化--format()方法-----https://b ...

  7. 漫谈NIO(1)之计算机IO实现

    1.前言 此系列将尽可能详细介绍断更博客半年以来个人的一个成长,主要是对Netty的源码的一个解读记录,将从整个计算机宏观IO体系上,到Java的原生NIO例子最后到Netty的源码解读.不求完全掌握 ...

  8. 在Mac OS X上启用Apache和PHP

    因为Mac OS X上都已自带了Apache和PHP,所以都无需进行安装,只要按照自己的需要进行设置即可. 找到httpd.conf文件,并用编辑器打开. 加载PHP模块.找到 #LoadModule ...

  9. Form表单如何可以传递多个值传递List数组对象到后台的解决办法

    举例说明: 后台有一个对象 User ,结构如下: 后台有一个对象 User ,结构如下: public class User{ private String username; private Li ...

  10. Android Layout 01_activity_Login.xml

    activity_login.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...