HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
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.
昨晚,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 Ⅱ(噩梦 Ⅱ)的更多相关文章
- HDU - 3085 Nightmare Ⅱ
HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...
- 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)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- [hdu P3085] Nightmare Ⅱ
[hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【HDU 3085】 Nightmare Ⅱ
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3085 [算法] 双向BFS [代码] #include<bits/stdc++.h> ...
- Nightmare Ⅱ HDU - 3085 (双向bfs)
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...
- 【HDU - 3085】Nightmare Ⅱ(bfs)
-->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...
随机推荐
- 学习lambda表达式总结
因为最近开发涉及到大量的集合数据处理,就开始研究lambda表达式使用,看了<Java8函数式编程>,同时研究了不少博客,总结了一些基础的用法,写一篇博客,为以后的使用提供便利. 下面介绍 ...
- springcloud第五步:使用Zuul搭建服务接口网关
路由网关(zuul) 什么是网关 Zuul的主要功能是路由转发和过滤器.路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务.zuul默认和Ri ...
- spring batch (四) Job的配置及配置文件说明介绍
内容来自<Spring Batch 批处理框架>,作者:刘相.我只是个搬运工. 一.Spring Batch提供了独立的标签用来顶一个Job配置,分别是job.step.tasklet.c ...
- [js]javascript索引
js&jq总结的还是挺棒的: http://www.cnblogs.com/yuanchenqi/articles/5980312.html http://www.cnblogs.com/yu ...
- charles-抓包Andriod 手机的设置
长按弹出 修改后: charles如果不配置SSL通用证书: 会导致HPPTS协议的域名抓取失败/乱码的现象: 现在SSL越来越多,很多博客都上了SSL,支付相关的行业更是基础配置: charles配 ...
- mac电脑安装wxPython2.8.12.1不成功怎么办 , Could not find a version that satisfies the requirement 2.8.12.1
目的:robotframe-ride用于接口测试 遇到的问题: 1.mac终端pip安装robotframework-ride后 pip install robotframework-ride (pi ...
- mysql导入本地文件(作业)
1.准备本地文件(pet.txt) 2.在CMD中启动mysql服务,然后输入以下命令导入(pet.txt) load data local infile '路劲' into table pet; 3 ...
- Pyenv部署
一.Git克隆方式 1.安装git yum -y install git 2.克隆pyenv到本地 git clone https://github.com/pyenv/pyenv.git ~/.py ...
- 2017UGUI之slider
不让鼠标控制slider的滑动: 鼠标之所以可以控制滑动是因为slider具有interactable这个属性(下图红色的箭头的地方):如果取消了这个属性的运行的时候就不能滑动了.如果要代码去控制这个 ...
- C语言 全局变量、静态全局变量、局部变量、静态局部变量
//test.c #include <stdio.h> extern int global_var; void test_global_var() { global_var++; prin ...