HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

 

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.
昨晚,erriyue经历了一场可怕的噩梦。他梦见自己和女朋友被困在一座大迷宫中。更糟的是,还有两杀人恶鬼环伺其中。现在erriyue想知道自己是否能先于恶鬼找到女朋友。
你可以认为erriyue和他女朋友可以移动4个方向。每回合,erriyue可以移动3步且他女朋友可以移动1步。恶鬼比较溜,可以随意分裂并不断占据2步内的格子,直到覆盖迷宫。每回合鬼先动,人再动,若走到有鬼的格子,卒。
注意:新旧鬼皆能分裂。

CN

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.

输入开头为整数T,表示测试用例数。
每组测试用例头一行有两个整数n和m,表示迷宫大小。(<n, m<)
随后n行迷宫。每行m个字符,表意如下:
‘.’ 空格,谁都能走。
‘X’ 墙,唯鬼能过。
‘M’ erriyue。
‘G’ 女朋友。
‘Z’ 恶鬼。
数据保证仅有一个M,一个G和两个Z。

CN

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.

如果erriyue 和女朋友可以在最短时间S成功相遇,则输出整数S在单独一行;否则输出-。

CN

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

题解

  两层BFS(当然双向也可以)

  人可以等人,人不能穿鬼,鬼比人快1s……

  然后把两层结果比较一下就行了。

代码 C++

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#define MX 805
#define INF 0x7F7F7F7F
struct Point {
int y, x;
}M, G, Z[], now, nxt;
int data[MX][MX][], drc[] = { -, , , , , -, , };
bool isCatch(int y, int x, int tim) {
return std::min(abs(y - Z[].y) + abs(x - Z[].x), abs(y - Z[].y) + abs(x - Z[].x)) <= tim * ;
}
void BFS() {
int i = , j, tim, tmp;
std::queue<Point> q;
for (i = ; i < ; ++i) {
i ? q.push(G) : q.push(M);
while (!q.empty()) {
now = q.front(); q.pop();
tim = data[now.y][now.x][i] + ;
tmp = i ? tim : (tim + ) / ;
if (isCatch(now.y, now.x, tmp)) continue;
for (j = ; j < ; j += ) {
nxt.y = now.y + drc[j]; nxt.x = now.x + drc[j + ];
if (isCatch(nxt.y, nxt.x, tmp) || data[nxt.y][nxt.x][i] <= tim) continue;
data[nxt.y][nxt.x][i] = tim;
q.push(nxt);
}
}
}
}
int main() {
char str[MX];
int t, i, j, n, m, iz, opt, tim;
scanf("%d", &t);
while (t--) {
scanf("%d%d ", &n, &m);
memset(data, -, sizeof data); iz = ;
for (i = ; i < n; ++i) {
gets(str);
for (j = ; j < m; ++j) {
switch (str[j]) {
case '.': data[i + ][j + ][] = data[i + ][j + ][] = INF; break;
case 'Z': Z[iz].y = i + ; Z[iz].x = j + ; ++iz; break;
case 'M': M.y = i + ; M.x = j + ; break;
case 'G': G.y = i + ; G.x = j + ; break;
}
}
}
data[M.y][M.x][] = data[G.y][G.x][] = INF;
data[M.y][M.x][] = data[G.y][G.x][] = ;
BFS();
opt = INF;
for (i = ; i <= n; ++i) {
for (j = ; j <= m; ++j) {
if ((data[i][j][] | data[i][j][]) == -) continue;
tim = std::max((data[i][j][] + ) / , data[i][j][]);
opt = std::min(opt, tim);
}
}
opt > MX ? puts("-1") : printf("%d\n", opt);
}
return ;
}

HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)的更多相关文章

  1. HDU - 3085 Nightmare Ⅱ

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

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

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

  3. HDU 3085 Nightmare Ⅱ(双向BFS)

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

  4. HDU 3085 Nightmare Ⅱ (双向BFS)

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

  5. HDU 3085 Nightmare Ⅱ 双向BFS

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

  6. [hdu P3085] Nightmare Ⅱ

    [hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

  7. 【HDU 3085】 Nightmare Ⅱ

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3085 [算法] 双向BFS [代码] #include<bits/stdc++.h> ...

  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)

    -->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...

随机推荐

  1. group by 用法解析

    group by 用法解析 group by语法可以根据给定数据列的每个成员对查询结果进行分组统计,最终得到一个分组汇总表. SELECT子句中的列名必须为分组列或列函数.列函数对于GROUP BY子 ...

  2. 在windows上搭建git server Gitblit

    在Windows上搭建Git Server   第1步:下载Java并安装Java.我这里下载的是jdk1.7.0_79 第2步:配置Java环境变量 右键” 计算机” => ”属性” => ...

  3. 简单的C++输出日志

    myLog.h #ifndef __myLog_H_ #define __myLog_H_ #include <stdio.h> #include <stdlib.h> #in ...

  4. BigDecimal源码

    1 public BigDecimal(char[] in, int offset, int len, MathContext mc) {// 使用字符数组的构造方法,一般我们推荐使用的是一Strin ...

  5. log4net 写入 mongodb+Mongodb记录日志

    项目里面需要记录大量的日志,为了方便分析,也是为了方便开发人员远程查询日志,可以把日志写入MongoDB. 1.先上Log4net配置 <?xml version="1.0" ...

  6. ASP.NET MVC案例教程(七)

      前言 写这篇文章的目的,是想总结一些东西,以帮助朋友们更好的使用这个框架.但是,我又不像把官方列举的哪些优势.功能翻译过来列举在这里.所以,我想干脆我就纯从个人观点上对这个框架评论一下吧.说的不好 ...

  7. Vue系列之 => webpack-babel的配置

    安装 cnpm i babel-core@6.26.3 babel-loader@7.0.0 babel-plugin-transform-runtime -D cnpm i babel-preset ...

  8. C#-----线程安全的ConcurrentQueue<T>队列

     ConcurrentQueue<T>队列是一个高效的线程安全的队列,是.Net Framework 4.0,System.Collections.Concurrent命名空间下的一个数据 ...

  9. Requests卡死问题

    https://www.cnblogs.com/niansi/p/7143736.html https://blog.csdn.net/pilipala6868/article/details/807 ...

  10. Fiddler使用教程(转)

    Fiddler是最强大最好用的Web调试工具之一,你对HTTP协议越了解, 你就能越掌握Fiddler的使用方法.你越使用Fiddler,就越能帮助你了解HTTP协议.Fiddler无论对开发人员或者 ...