BFS(双向) HDOJ 3085 Nightmare Ⅱ
题意:一个人去救女朋友,两个人都在运动,还有鬼在"扩散",问最少几秒救到女朋友
分析:开两个队列来表示两个人走过的路,一个人走到的地方另一个人已经vis了,那么就是相遇了,鬼就用曼哈顿距离判断.
#include <bits/stdc++.h>
using namespace std; const int N = 8e2 + 5;
char maze[N][N];
int n, m;
bool vis[N][N][2];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
struct Point {
int x, y;
Point() {}
Point(int x, int y) : x (x), y (y) {}
};
queue<Point> que[2];
Point mm, gg, gho[2]; int get_dis(int x, int y, int ex, int ey) {
return abs (ex - x) + abs (ey - y);
} bool check2(int x, int y, int tim) {
for (int i=0; i<2; ++i) {
if (get_dis (x, y, gho[i].x, gho[i].y) <= 2 * tim) return false;
}
return true;
} bool check(int x, int y, int typ) {
if (x < 1 || x > n || y < 1 || y > m || maze[x][y] == 'X') return false;
else return true;
} void init(void) {
while (!que[0].empty ()) que[0].pop ();
while (!que[1].empty ()) que[1].pop ();
memset (vis, false, sizeof (vis));
} bool BFS(int typ, int tim) {
int sz = que[typ].size ();
while (sz--) {
Point u = que[typ].front (); que[typ].pop ();
if (!check (u.x, u.y, typ) || !check2 (u.x, u.y, tim)) continue;
for (int i=0; i<4; ++i) {
int tx = u.x + dx[i], ty = u.y + dy[i];
if (!check (tx, ty, typ) || !check2 (tx, ty, tim)) continue;
if (vis[tx][ty][typ^1]) return true;
if (vis[tx][ty][typ]) continue;
vis[tx][ty][typ] = true;
que[typ].push (Point (tx, ty));
}
}
return false;
} int run(void) {
init ();
vis[mm.x][mm.y][0] = true;
vis[gg.x][gg.y][1] = true;
que[0].push (Point (mm.x, mm.y));
que[1].push (Point (gg.x, gg.y));
int step = 0;
while (!que[0].empty () || !que[1].empty ()) {
++step;
for (int i=0; i<3; ++i) {
if (BFS (0, step)) return step;
}
if (BFS (1, step)) return step;
}
return -1;
} int main(void) {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf ("%s", maze[i] + 1);
}
int t = 0;
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
if (maze[i][j] == 'M') {
mm.x = i; mm.y = j;
}
else if (maze[i][j] == 'G') {
gg.x = i; gg.y = j;
}
else if (maze[i][j] == 'Z') {
gho[t].x = i; gho[t++].y = j;
}
}
}
printf ("%d\n", run ());
} return 0;
}
BFS(双向) HDOJ 3085 Nightmare Ⅱ的更多相关文章
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU - 3085 Nightmare Ⅱ
HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...
- UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)
题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...
- UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)
题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- 【HDOJ】3085 Nightmare Ⅱ
双向BFS.注意,任何一个点出队后,首先需要考虑ghost. /* 3085 */ #include <iostream> #include <queue> #include ...
随机推荐
- NYOJ题目611练练
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAssAAAJ1CAIAAACgqiqJAAAgAElEQVR4nO3du27jSp4HYL+Ecj2IYz
- 恶趣味小游戏 I'm hungry
之前学算法的时候无聊做了个游戏放松放松,现在传到了github以免电脑坏了就永远丢失了... github地址:https://github.com/BenDanChen/IamHungry I am ...
- 与你相遇好幸运,Waterline初遇
Github : https://github.com/balderdashy/waterline 文档 : https://github.com/balderdashy/waterline-docs ...
- Dubbo应用与异常记录
结合项目里使用暴露出的问题,对并发较多的核心业务或者对请求失败等敏感的业务场景不太建议使用Dubbo, 如电商的购买等行为,使用Dubbo就必须阅读源码,熟悉相关机制,或者直接自己造轮子. >& ...
- acpi参考网站
1.acpi官网: http://www.acpi.info/
- JavaWeb学习之tomcat安装与运行、tomcat的目录结构、配置tomcat的管理用户、web项目目录、虚拟目录、虚拟主机(1)
1.tomcat安装与运行双击tomcat目录下的bin/startup.bat,启动之后,输入http://localhost:8080,出现安装成功的提示,表示安装tomcat成功 2.tomca ...
- MVC中数据的内部校验
针对MVC中实例类型的修饰,我们可以采用DataAnnotations类来完成,该类所处的命名空间是System.ComponentModel.DataAnnotations; 一.通过开类型,能够修 ...
- 【JAVA IO流之字节流】
字节流部分和字符流部分的体系架构很相似,有四个基本流:InputStream.OutputStream.BufferedInputStream.BufferedOutputStream,其中,Inpu ...
- ytu 1059: 判别该年份是否闰年(水题,宏定义)
1059: 判别该年份是否闰年 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 222 Solved: 139[Submit][Status][Web ...
- GMap.Net开发之自定义Marker
上一篇文章介绍了如何在WinForm和WPF中使用GMap控件,这篇介绍下GMap中Marker的使用. 自定义Marker,可以理解为在地图上自定义图标(Custom Marker),先看看GMap ...