题目传送门

题意:一个人去救女朋友,两个人都在运动,还有鬼在"扩散",问最少几秒救到女朋友

分析:开两个队列来表示两个人走过的路,一个人走到的地方另一个人已经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 Ⅱ的更多相关文章

  1. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. HDU - 3085 Nightmare Ⅱ

    HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...

  3. UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)

    题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...

  4. UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)

    题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

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

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

  6. HDU 3085 Nightmare Ⅱ (双向BFS)

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

  7. HDU 3085 Nightmare Ⅱ(双向BFS)

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

  8. HDU 3085 Nightmare Ⅱ 双向BFS

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

  9. 【HDOJ】3085 Nightmare Ⅱ

    双向BFS.注意,任何一个点出队后,首先需要考虑ghost. /* 3085 */ #include <iostream> #include <queue> #include ...

随机推荐

  1. 如何在yii的controller中调用外部action

    问题: 在yii中,一个controller会包含若干个action.有时为了重用或代码管理等目的,我们希望这些action可以单独定义成一个类,然后在controller中使用.那么在yii中要如何 ...

  2. asp.net 曲线图

    public void draw(DataTable dt) { //取得记录数量 int count = dt.Rows.Count; //记算图表宽度 int wd = 80 + 20 * (co ...

  3. Windows 删除 .svn标志

    之前一个项目是在SVN下面管理的,后来,考出来了,然后在Eclispe中使用,后来想用SVN管理起来,但是项目中,还是有.svn标志,只能先删除了.svn文件,然后在用svn管理起来,后来,发现.sv ...

  4. C --gettimeofday===获取某个程序经历的时间

    #include <stdio.h> #include <sys/time.h> #include <time.h> int gettimeofday(struct ...

  5. Android 更改字体

    1. 将字体ttf文件放在assets目录下 2. 使用: Typeface mTypeFaceLight = Typeface.createFromAsset(context.getAssets() ...

  6. struts2中一些常用的写法 记录

    1.对日期进行处理 Date current = new Date(); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ...

  7. AngularJS 服务(Service)

    AngularJS 中你可以创建自己的服务,或使用内建服务. 什么是服务? 在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用. AngularJS 内建了30 ...

  8. 与你相遇好幸运,Waterline初遇

    Github : https://github.com/balderdashy/waterline 文档 : https://github.com/balderdashy/waterline-docs ...

  9. 设计模式学习之代理模式(Proxy,结构型模式)(11)

    参考地址:http://www.cnblogs.com/zhili/p/ProxyPattern.html 一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访 ...

  10. python threading编程中的LOCK和RLOCK(可重入锁)

    找到一本PYTHON并发编辑的书, 弄弄.. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time sh ...