题目:

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. 

输入:

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

样例:

分析:cin cout加std::ios::sync_with_stdio(false);都过不去(/‵Д′)/~ ╧╧,换scanf就ac了?!

双向BFS,预处理鬼什么时候覆盖该位置(貌似博客题解都是用曼哈顿距离?明明预处理更明显想到(ctrl c + v?))

记住鬼在人前行动,对照样例3理解这句话

 #include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cctype>
#define PI acos(-1.0)
const int INF = 0x3f3f3f3f;
const int NINF = -INF - ;
typedef long long ll;
using namespace std;
typedef pair<int, int> P;
char maze[][];
int n, m;
int mx, my, gx, gy;
P z[];
int timz[][];
int visz[][];
int dzx[] = {, , , , -, -, , , , , -, -}, dzy[] = {, , , , , , -, -, , -, , -};
void ini()
{
for (int i = ; i < n; ++i)
{
for (int j = ; j < m; ++j)
timz[i][j] = INF;
}
for (int i = ; i < n; ++i)
{
for (int j = ; j < m; ++j)
visz[i][j] = ;
}
queue<P> p;
timz[z[].first][z[].second] = ;
timz[z[].first][z[].second] = ;
visz[z[].first][z[].second] = ;
visz[z[].first][z[].second] = ;
p.push(z[]);
p.push(z[]);
while (p.size())
{
P tmp = p.front();
p.pop();
for (int i = ; i < ; ++i)
{
int nx = tmp.first + dzx[i], ny = tmp.second + dzy[i];
if (nx >= && nx < n && ny >= && ny < m && !visz[nx][ny])
{
visz[nx][ny] = ;
timz[nx][ny] = timz[tmp.first][tmp.second] + ;
p.push(P(nx, ny));
}
}
}
}
int dx[] = {, , -, }, dy[] = {, , , -};
int vis[][][];
queue<P> q[];
int step;
int bfs(int flag)
{
int num = q[flag].size();
while (num--)
{
P tmp = q[flag].front();
q[flag].pop();
if (step >= timz[tmp.first][tmp.second]) continue;
for (int i = ; i < ; ++i)
{
int nx = tmp.first + dx[i], ny = tmp.second + dy[i];
if (nx < || nx >= n || ny < || ny >= m || vis[flag][nx][ny] || maze[nx][ny] == 'X' || step >= timz[nx][ny])
continue;
if (vis[ - flag][nx][ny])
{
printf("%d\n", step);
return ;
}
vis[flag][nx][ny] = ;
q[flag].push(P(nx, ny));
}
}
return ;
}
void solve()
{
for (int i = ; i < ; ++i)
{
while (q[i].size()) q[i].pop();
}
memset(vis[], , sizeof(vis[]));
memset(vis[], , sizeof(vis[]));
vis[][mx][my] = ;
vis[][gx][gy] = ;
q[].push(P(mx, my));
q[].push(P(gx, gy));
step = ;
while (q[].size() || q[].size())
{
step++;
for (int i = ; i < ; ++i)
if (bfs()) return;
if (bfs()) return;
}
printf("-1\n");
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d%d",&n,&m);
int num = ;
for (int i = ; i < n; ++i)
{
scanf("%s",maze[i]);
for (int j = ; j < m; ++j)
{
if (maze[i][j] == 'M') mx = i, my = j;
if (maze[i][j] == 'G') gx = i, gy = j;
if (maze[i][j] == 'Z') z[num].first = i, z[num++].second = j;
}
}
ini();
solve();
}
return ;
}

HDU3085 Nightmare Ⅱ的更多相关文章

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

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

  2. HDU3085 Nightmare Ⅱ (双向BFS)

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

  3. 【HDU - 3085】Nightmare Ⅱ(bfs)

    -->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...

  4. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

  5. Nightmare基于phantomjs的自动化测试套件

    今天将介绍一款自动化测试套件名叫nightmare,他是一个基于phantomjs的测试框架,一个基于phantomjs之上为测试应用封装的一套high level API.其API以goto, re ...

  6. POJ 1984 Navigation Nightmare 带全并查集

    Navigation Nightmare   Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

  7. Nightmare

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

  9. HDU 3085 Nightmare Ⅱ (双向BFS)

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

随机推荐

  1. Python培训时长多久可以学会?马哥教育9年经验之谈

    在Python成为一门炙手可热的语言之后,很多人也开始准备向这个行业发展.技术入行也就是培训和自学两条路,各有优劣,不过培训因为学习比较系统比较快也受到不少人欢迎. 今天我就来给大家分享一下Pytho ...

  2. bootstrap table分页(前后端两种方式实现)

    bootstrap table分页的两种方式: 前端分页:一次性从数据库查询所有的数据,在前端进行分页(数据量小的时候或者逻辑处理不复杂的话可以使用前端分页) 服务器分页:每次只查询当前页面加载所需要 ...

  3. 集成学习_Bagging 和随机森林(rf)

       集成学习方式总共有3种:bagging-(RF).boosting-(GBDT/Adaboost/XGBOOST).stacking      下面将对Bagging 进行介绍:(如下图所示) ...

  4. P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools

    P2746 [USACO5.3]校园网Network of Schools// POJ1236: Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学 ...

  5. HDU-1864&&HDU-2602(01背包问题)

    DP-01背包问题例题 输入处理有点恶心人,不过处理完后就是简单的DP了 从头开始dp[i]表示从0开始到i的最优结果,最后从都边里dp数组,求得最大的报销额. 对于每个i都要从头维护最优结果.(二刷 ...

  6. BZOJ 1631 Usaco 2007 Feb. Cow Party

    [题解] 最短路裸题.. 本题要求出每个点到终点走最短路来回的距离,因此我们先跑一遍最短路得出每个点到终点的最短距离,然后把边反向再跑一遍最短路,两次结果之和即是答案. #include<cst ...

  7. 简单的SpringBoot环境搭建

    开始搭建前请确认您的计算机中的Maven已正确配置 一:使用IDEA创建一个Maven项目,图中第一个指针请选择自己正在使用的JDK版本,指针二请打勾,选中指针三所指向的类型并点击Next 二:填写G ...

  8. hdu 4975 最大流解决行列和求矩阵问题,用到矩阵dp优化

    //刚开始乱搞. //网络流求解,如果最大流=所有元素的和则有解:利用残留网络判断是否唯一, //方法有两种,第一种是深搜看看是否存在正边权的环,见上一篇4888 //至少四个点构成的环,第二种是用矩 ...

  9. FORTIFY_SOURCE

    In recent years Linux distributions started treating security more seriously. Out of many security f ...

  10. JDK工具jstatd用法详解(转)

    jstatd,即虚拟机的jstat守护进程,主要用于监控JVM的创建与终止,并提供一个接口允许远程监控工具依附到在本地主机上运行的JVM. 用法摘要 jstatd [ options ] option ...