题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085

Nightmare Ⅱ

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

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
 
Author
二日月
 
Source

题意:

1.Z一秒可以扩散到两步以内的范围,每一步都可以走四个方向, 可以穿墙。

2.M一秒可以走0123步, 每一步都可以是四个方向, 不可穿墙。

3.G一秒可以走01步, 每一步都可以是四个方向, 不可穿墙。

题解:

1.M和G是否可以相遇,典型的双向BFS问题。

2.由于ghost可以穿墙, 所以对于整个图来说,ghost畅通无阻, 因此可以利用曼哈顿距离来判断M和G是否会被抓到,避免了ghost加入队列的麻烦。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; char maze[MAXN][MAXN];
int n, m, dir[][] = {,,,,-,,,-};
int ghost[][]; struct node
{
int x, y;
};
node M, G; bool caught(node now, int time) //判断会不会被ghost抓住,使用曼哈顿距离判断
{
if(abs(now.x-ghost[][])+abs(now.y-ghost[][])<=*time) return true;
if(abs(now.x-ghost[][])+abs(now.y-ghost[][])<=*time) return true;
return false;
} queue<node>q[];
bool vis[][MAXN][MAXN];
bool bfs(int id, int time)
{
int Size = q[id].size(); //用于刹车,防止把新入队的点也进行更新
node now, tmp;
while(Size--)
{
now = q[id].front();
q[id].pop(); if(caught(now, time)) continue; //欲出发的点会被ghost抓住, 下一个
for(int i = ; i<; i++)
{
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
if(tmp.x>= && tmp.x<=n && tmp.y>= && tmp.y<=m //没出界
&& maze[tmp.x][tmp.y]!='X' && maze[tmp.x][tmp.y]!='Z' //可行通道
&& !vis[id][tmp.x][tmp.y] && !caught(tmp, time)) //没有被访问过,其此时此地不会被抓
{
vis[id][tmp.x][tmp.y] = true;
//对方已经访问过,则meet。不需判断两者到达的时间是否相等,因为可以走0步,所以就可以一直停留
if(vis[!id][tmp.x][tmp.y]) return true;
q[id].push(tmp); }
}
}
return false;
} int solve()
{
ms(vis, );
while(!q[].empty()) q[].pop();
while(!q[].empty()) q[].pop();
vis[][M.x][M.y] = ;
vis[][G.x][G.y] = ;
q[].push(M);
q[].push(G); int time = ; //计时器
while(!q[].empty() || !q[].empty())
{
time++;
for(int i = ; i<=; i++) //M在一秒内,可以走0、1、2、3步
if(bfs(, time)) return time;
if(bfs(, time)) return time; //G只能走0、1步
}
return -;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = ; i<=n; i++)
scanf("%s", maze[i]+); int cnt = ;
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
{
if(maze[i][j]=='Z') ghost[cnt][] = i, ghost[cnt++][] = j;
if(maze[i][j]=='M') M.x = i, M.y = j;
if(maze[i][j]=='G') G.x = i, G.y = j;
}
cout<< solve() <<endl;
}
}

HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离的更多相关文章

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

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

  2. HDU3085 Nightmare Ⅱ (双向BFS)

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

  3. HDU 3085 Nightmare Ⅱ 双向BFS

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

  4. HDU 3085 Nightmare Ⅱ (双向BFS)

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

  5. HDU3085(KB2-G 双向bfs)

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

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

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

  7. Nightmare Ⅱ(双向BFS)

    Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and his ...

  8. Nightmare Ⅱ HDU - 3085 (双向bfs)

    Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...

  9. HDU 3085 Nightmare Ⅱ(双向BFS)

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

随机推荐

  1. Python脚本实现值更新事件赋值过程记录日志监控

    #LogWatch 实现值更新事件赋值过程记录日志监控 clr.AddReference('System') clr.AddReference('Kingdee.BOS') clr.AddRefere ...

  2. 美丽的大树(codevs 2124)

    题目描述 Description 平江路是苏州最美丽的道路,路中间的绿化带上种了两行漂亮的大树,每行50棵,一共100棵大树,这些大树被编上了号,编号方式如下: 1 3 5 7 ………… 45 47 ...

  3. 洛谷 P 1330 封锁阳光大学

    题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构成的无向图,N个点之间由M ...

  4. msp430项目编程40

    msp430综合项目---多路温度检测系统40

  5. MYsql 锁详解 锁 与索引的关系

    原文:http://blog.csdn.net/xifeijian/article/details/20313977#t10   mysql innodb的锁是通过锁索引来实现的.   select ...

  6. c++ 高效并发编程

    高效并发编程 并发编程的基本模型包括,通过消息机制来管理运行顺序的message passing, 通过互斥保护共享的shared memory. 线程同步的基本原则 最低限度共享变量,考虑使用imm ...

  7. Jenkins持续部署-Windows环境持续部署探究1

    目录 Jenkins持续部署-Windows环境持续部署探究1 目录 前言 目的 方案流程 技术实现 PowerShell FTP上传插件 环境变量插件 脚本执行 远程调用 升级服务 启动服务 总结 ...

  8. IntelliJ IDEA设置properties文件显示中文

    配置这里: 注意:上面是Default Settings,还需要在Settings中设置成上面一样的.

  9. oracle-统计员工x

    1. SELECTe.depid,avg(s.bonussalary+s.basesalary) AS avgsal from employ e,salary s where e.employId=s ...

  10. iOS release版本去除NSLog打印信息

    因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉. 我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时 ...