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,可以走重复路,求出迷宫最短时 ...
随机推荐
- 《Linux及安全》实践3.3
<Linux及安全>实践三 字符集总结与分析 [by lwr] 一.ISO.UCS/UTF.GB系列字符集分析 1.字符集&字符编码 字符集(Charset):是一个系统支持的所有 ...
- vim - char code and charset
In normal mode, type ga to display the decimal and hex values for the character under the cursor, or ...
- Java c3p0连接池之二
<?xml version="1.0" encoding="UTF-8"?> <!-- c3p0-config.xml文件配置 --> ...
- 创建redis集群
假设你已经安装好了redis ,如果还没有请安装 将多个实例跑起来 创建一个目录,比如 redis-cluster 把redis-server拷贝到这个目录下 在目录下为每一个实例创建一个文件夹 在每 ...
- lua table integer index 特性
table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...
- RDIFramework.NET ━ .NET快速信息化系统开发框架 记录所有操作的Sql
在实际开发或试运行过程中,我们有时需要查看或分析模块执行的所有sql,以便进行相关分析.有时我们可以通过数据库自带的软件抓取,如:SQL Server Profiler.在我们RDIFramework ...
- R12.2.6 installation failed with - Unable to rename database
报错信息: 日志信息:/data/ebsdb/VIS/12.1.0/appsutil/log/VIS_ebstest/12222150.log Phase 3 Rename Database Exec ...
- tftp 限制ip 限制ip段 或者多个ip段访问
1 限制单个ip访问 tftp 配置tftp信息 vi /etc/xinetd.d/tftp 在 service tftp配置信息中添加 only_form =ip 重启 service xinet ...
- Memcached,你懂的
一.Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网 ...
- Django知识(二)
上一部链接 django入门全套(第一部) 本章内容 Django model Model 基础配置 django默认支持sqlite,mysql, oracle,postgresql数据库. < ...