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 ...
随机推荐
- Rabbit MQ 消息确认和持久化机制
一:确认种类 RabbitMQ的消息确认有两种.一种是消息发送确认,用来确认生产者将消息发送给交换器,交换器传递给队列的过程中消息是否成功投递.发送确认分为两步,一是确认是否到达交换器,二是确认是否到 ...
- 常见查找算法(Java代码实现)
一,顺序查找 查找算法中顺序查找算是最简单的了,无论是有序的还是无序的都可以,只需要一个个对比即可,但其实效率很低.我们来看下代码 public static int search(int[] a, ...
- php----------const 定义的常量和define()定义的常量的区别?
用法一:const用于类成员变量,一经定义不可修改,define用于全局常量,不可用于类成员变量的定义,const可在类中使用也可以在类外面使用,define不能. 定义:const 常量名=值; 没 ...
- Redis.之.环境搭建(集群)
Redis.之.环境搭建(集群) 现有环境: /u01/app/ |- redis # 单机版 |- redis-3.2.12 # redis源件 所需软件:redis-3.0.0.gem -- ...
- (.NET高级课程笔记)泛型总结
泛型总结 1.引入泛型:延迟声明,即在声明的时候没有指定参数类型,只有当调用的时候才会确定 其参数类型(架构师的理念:推迟一切可以推迟的) 2.如何声明和使用泛型 3.泛型的好处和原理 4.泛型类.泛 ...
- 接口自动化测试持续集成--Soapui接口功能测试断言
断言也就是判断实际结果与预期结果是否相等,如果相等测试通过,否则测试失败,自动化测试不管是UI,Services还有unit都需要做断言. 一.添加断言步骤的组件 二.设置断言 设置常用断言的三种方式 ...
- 利用Oracle Database Resource Manager实现UNDO表空间的quota
1.查出当前使用的是哪个resource plan select * from GV$RSRC_PLAN 2.创建pending area begin dbms_resource_manager.c ...
- Spring boot 源码分析(前言)
开坑达人 & 断更达人的我又回来了 翻译的坑还没填完,这次再开个新坑= = 嗯,spring boot的源码分析 本坑不打算教你怎么用spring boot = = 也不打算跟你讲这玩意多方便 ...
- python locust 性能测试:HOOKS<钩子方法>
为locust中不同类型的事件,提供的钩子方法: from locust import TaskSet, task, events, Locust from locust.clients import ...
- JAVA函数的重载和重写
一.什么是重载(overlording) 在JAVA中,可以在同一个类中存在多个函数,函数名称相同但参数列表不同.这就是函数的重载(overlording).这是类的多太性表现之一. 二.重载的作用: ...