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 ...
随机推荐
- [Android ] linux命令英文缩写的含义(方便记忆)
du -sh */ reference to : http://blog.chinaunix.net/uid-27164517-id-3299073.html linux常用命令的英文单词缩写 命令缩 ...
- 树形DP习题
听闻noip要考树形DP,本蒟蒻万分惶恐,特刷一坨题目,以慰受惊之心. codevs 1486 /*和非常出名的"选课"是一个题*/ #include<cstdio> ...
- Mysql分布式事务
关于Mysql分布式事务介绍,可参考:http://blog.csdn.net/luckyjiuyi/article/details/46955337 分为两个阶段:准备和执行阶段.有两个角色:事务的 ...
- NYOJ题目839合并
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAKgCAIAAADmrHcoAAAgAElEQVR4nO3dO1LsOheG4X8S5AyE2A
- 1.4 算法 - algorithm
1)概述 2)示例 //algorithm find演示 #include <vector> #include <algorithm> #include <iostrea ...
- 动手动脑及java程序之用消息框进行数的运算
动手动脑 自信成就人生 动手动脑1 ✿仔细阅读示例: EnumTest.java,运行它,分析运行结果? package demo; public class Test { publi ...
- 对Cookie和Session的深入理解
session对象 session对象用于存储特定的用户会话所需的信息 . Session对象的引入是为了弥补HTTP协议的不足.HTTP协议本身是无状态的,这与HTTP协议本来的目的是相符的,客户端 ...
- ArcGIS图层和要素的过滤显示
ArcGIS可以设置动态地图服务(ArcGISDynamicMapServiceLayer)显示哪些图层,也可以设置每个图层根据某个属性字段的某些条件来进行过滤显示. 1.设置显示的图层 主要是通过A ...
- 命令行登陆Oracle(包括远程登陆)
本方法适用于在cmd命令行窗口以及pl/sql登陆Oracle下登录本机或者远程Oracle. 1.首先保证在当前主机上设置了ORACLE_HOME环境变量: 例如:ORACLE_HOME=D ...
- gitlab安装部署
参考文章: https://gitlab.com/gitlab-org/gitlab-recipes/tree/master/install/centos http://www.xsjxmx.com/ ...