HDU 1072 Nightmare
Description
Given the layout of the labyrinth and Ignatius' start
position, please tell Ignatius whether he could get out of the
labyrinth, if he could, output the minimum time that he has to use to
find the exit of the labyrinth, else output -1.
Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest
area, and he should not walk out of the border, of course he could not
walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains
Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the
equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you
wish, if it is needed, Ignatius can get to any areas in the labyrinth as
many times as you wish.
6. The time to reset the exploding time can be ignore, in
other words, if Ignatius get to an area which contain
Bomb-Rest-Equipment, and the exploding time is larger than 0, the
exploding time would be reset to 6.
Input
single integer T which is the number of test cases. T test cases
follow.
Each test case starts with two integers N and M(1<=N,Mm=8)
which indicate the size of the labyrinth. Then N lines follow, each
line contains M integers. The array indicates the layout of the
labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.
Output
should output the minimum time he needs, else you should just output -1.
Sample Input
Sample Output
题目有点长呀~~读懂题目其实就好做了。
题目大意就是:在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器。定时炸弹的时间是6,人走一步所需要的时间是1。每次可以上、下、左、右移动一格。当人走到4时如果炸弹的时间不是0,可以重新设定炸弹的时间为6。如果人走到3而炸弹的时间不为0时,成功走出。求人从2走到3的最短时间。但是要注意的是:地图可以重复访问,但是实际上,如果是对于地图上是4的点,重复去访问的话,没有意义,因为如果走的出去,走一遍是4的就可以了,重复去走只会增加步数,而如果走不出去。。。更没必要了,所以,可以对4的点进行标记。
//Asimple
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#include <time.h>
using namespace std;
const int maxn = ;
int dx[] = {-,,,}, dy[]={,,-,};
typedef long long ll;
int n, m, num, T, k, x, y, len, ans;
int endx, endy;
int Map[maxn][maxn]; struct node{
int x;
int y;
int step;
int time;
};
node start; bool wrong(int x, int y) {
return x< || x>=n || y< || y>=m || !Map[x][y];
} void BFS() {
queue<node> q;
node now, next;
q.push(start);
while( !q.empty() ) {
now = q.front();
q.pop();
for(int i=; i<; i++) {
next.step = now.step+;
next.time = now.time-;
next.x = now.x + dx[i];
next.y = now.y + dy[i];
if( !wrong(next.x, next.y) && next.time> ) {
if( Map[next.x][next.y] == ) {
cout << next.step << endl;
return ;
} else if( Map[next.x][next.y] == ) {
next.time = ;
Map[next.x][next.y] = ;
}
q.push(next);
}
}
}
cout << - << endl;
} void input() {
cin >> T ;
while( T -- ) {
cin >> n >> m;
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
cin >> Map[i][j];
if( Map[i][j] == ) {
start.x = i;
start.y = j;
}
}
}
start.step = ;
start.time = ;
BFS();
}
} int main(){
input();
return ;
}
HDU 1072 Nightmare的更多相关文章
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- hdu - 1072 Nightmare(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...
- HDU 1072 Nightmare (广搜)
题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...
- HDU 1072 Nightmare 题解
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- [hdu P3085] Nightmare Ⅱ
[hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU - 3085 Nightmare Ⅱ
HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...
- HDU 1072 (不一样的入队条件) Nightmare
之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们 ...
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
随机推荐
- C++ 环形缓冲区的实现
参考文章:http://blog.csdn.net/linyt/article/details/53355355 本文参考linux系统中 kfifo缓冲区实现.由于没有涉及到锁,在多线程环境下,只适 ...
- 使用PowerShell解三道测试开发笔试题
在网上看到了三道测试开发的笔试题,答案是用Python解的.这段时间正好在学PowerShell,练习一下:) 1. 验证邮箱格式 2. 获取URL的后缀名 3. 获取前一天时间或前一秒 我的解法是: ...
- warnin php startup in unknown on line 0:
PHP Warning: PHP Startup: in Unknown on line 0 这种情况是因为扩展路径有问题导致的路径或错,或没有该扩展但ini中开启了此扩展
- Ubantu Linux 环境下编译c++程序
先在文件中新建一个a.cpp文件,在里面编写程序, 然后打开终端输入下面命令即可; $ g++ a.cpp -o b ///编译a.cpp 然后把编译之后的.exe文件存入b中 $ ./b ///执行 ...
- 自动更新补丁Security Update for Internet Explorer 10 for Windows Server 2008 R2 x64 Edition (KB2964358)失败
下载http://www.microsoft.com/zh-CN/download/details.aspx?id=42581手动安装成功.
- ActiveMQ简介
ActiveMQ 1.ActiveMQ是什么ActiveMQ是Apache推出的一款开源的完全支持JMS1.1和J2EE1.4规范的JMS Provider实现的消息中间件(Message Orien ...
- 简单的js菜单
<!DOCTYPE html> <html> <head> <title>hovertree</title><base target= ...
- Hibernate操作指南-搭建一个简单的示例(基于Java Persistence API JPA)
- Linux 搭建NTP服务器
NTP服务器[Network Time Protocol(NTP]是用来使计算机时间同步化的一种协议,NTP服务器可以对其它服务器做时间同步化,从而达到时间统一. 配置环境及要求: A. 假设10.8 ...
- Nginx location 匹配顺序整理
Nginx location模块整理 具体的Nginx安装就不在这里描述了,这里只是为了对location的描述 Nginx环境 a. 查看当前系统cat /etc/redhat-release [r ...