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 ...
随机推荐
- Quatre 2D的绘图功能的三个步骤(上下文,绘图,渲染)
一.qurza2d是怎么将绘图信息和绘图的属性绘制到图形上下文中去的? 说明: 新建一个项目,自定义一个view类和storyboard关联后,重写该类中的drowrect方法. 画线的三个步骤: ( ...
- 最简单的Web服务器
//读取浏览器发过来的内容Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, Protoco ...
- python以指定宽度及格式输出字符串
- JavaWeb学习之什么JSP、JSP是如何工作的、JSP语言(各种指令和九大内置对象)、EL表达式简单使用(5)
1.什么JSP * servlet:java编写的处理动态web的技术 * 特点:Java代码中嵌套html代码 * jsp * 特点:HTMl代码中嵌套java代码 * %tomcat%/conf/ ...
- Jquery 自定义弹窗等待
(function ($) { $.extend({ //弹窗蒙层 ShowLoadDialog : function () { ) { var cusrtxt = $("<div i ...
- python多进程程序之间交换数据的两种办法--Queue和Pipe
合在一起作的测试. #!/usr/bin/env python # -*- coding: utf-8 -*- import multiprocessing import random import ...
- ora-01658 :无法为表空间USERS 中的段创建INITIAL区
"CREATE INDEX "IDX_TS_BONUS_Q_201209_DS" ON "TS_BONUS_Q_201209" ("DS&q ...
- [LeetCode] TwoSum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- jq获取鼠标位置
jq获取鼠标位置 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- ASP.NET 5探险(6):升级ASP.NET 5到beta6
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:微软根据ASP.NET 5的路线图如期发布了beta6,现在我们就来说说beta5升级 ...