Nightmare
Nightmare
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5392 Accepted Submission(s): 2673
Problem Description
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.
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
The input contains several test cases. The first line of the input is a 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
For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.
Sample Input
3
3 3
2 1 1
1 1 0
1 1 3
4 8
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
5 8
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1
1 1 4 1 1 1 1 1
Sample Output
4
-1
13
Author
Ignatius.L
分析:bfs.状态描述:坐标x,y剩余时间t.因为x,y<=8,t<=6将之表示为一个三位数.
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
int state;
int time;
};
const int dx[]={-,,,};
const int dy[]={,,-,};
node S;
int N,M,map[][];
bool f[];
int bfs()
{
queue<node> q;
while (!q.empty()) q.pop();
memset(f,,sizeof(f));
S.time=;
q.push(S);
f[S.state]=;
while (!q.empty())
{
node u=q.front();
q.pop();
int x=u.state/,y=(u.state%)/,t=u.state%;
if (t==) continue;
if (map[x][y]==) return u.time;
if (t==) continue;
for (int i=;i<;i++)
if (<=x+dx[i] && x+dx[i]<=N && <=y+dy[i] && y+dy[i]<=M && map[x+dx[i]][y+dy[i]])
{
int x_=x+dx[i],y_=y+dy[i];
int v=x_*+y_*;
if (map[x_][y_]==) v+=;
else v+=t-;
if (!f[v])
{
node tmp;
tmp.state=v;
tmp.time=u.time+;
f[v]=;
q.push(tmp);
}
}
}
return -;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&N,&M);
for (int i=;i<=N;i++)
for (int j=;j<=M;j++)
{
scanf("%d",&map[i][j]);
if (map[i][j]==) S.state=i*+j*+;
}
printf("%d\n",bfs());
}
return ;
}
Nightmare的更多相关文章
- HDU 1072 Nightmare
Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...
- Nightmare基于phantomjs的自动化测试套件
今天将介绍一款自动化测试套件名叫nightmare,他是一个基于phantomjs的测试框架,一个基于phantomjs之上为测试应用封装的一套high level API.其API以goto, re ...
- POJ 1984 Navigation Nightmare 带全并查集
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- nyoj 483 Nightmare【bfs+优先队列】
Nightmare 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found him ...
- hdoj 1072 Nightmare
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- [Node.js] Scraping Dynamic JavaScript Websites with Nightmare
Many websites have more than just simple static content. Dynamic content which is rendered by JavaSc ...
随机推荐
- [Effective JavaScript 笔记]第18条:理解函数调用、方法调用及构造函数调用之间的不同
面向对象编程中,函数.方法.类的构造函数是三种不同的概念. JS中,它们只是单个构造对象的三种不同的使用模式. 三种不同的使用模式 函数调用 function hello(username){ ret ...
- 中位数与第K小元素
算法实际上是模仿快速排序算法设计出来的,其基本思想也是对输入数组进行递归划分,与快速排序不同的是,它只对划分出来的子数组之一进行递归处理: int randompartition(int a[],in ...
- 用VMware安装虚拟系统时出现Invalid system disk,Replace the disk and then press any key
VMware 默认是第一次从光盘启动,第二次从硬盘启动,你刚分区,里面还没有系统,当然报这个错,再次从光盘启动需要设置 VMware 的 BIOS,重新启动虚拟系统,当出现 VMware 的图标时用鼠 ...
- jquery博客收集的IE6中CSS常见BUG全集及解决方案
今天的样式调的纠结,一会这边一会那么把jquery博客折腾的头大,浏览器兼容性.晚上闲着收集一些常见IE6中的BUG 3像素问题及解决办法 当使用float浮动容器后,在IE6下会产生3px的空隙,有 ...
- SQL常见笔试面试题
sql理论题 1.触发器的作用? 答:触发器是一中特殊的存储过程,主要是通过事件来触发而被执行的.它可以强化约束,来维护数据的完整性和一致性,可以跟踪数据库内的操作从而不允许未经许可的更新和变化.可以 ...
- virsh常用命令
必须启动libvirtd,才能用virsh查看kvm后台. # systemctl start libvirtd 查看网络 # virsh net-list 启动default网络 # virsh n ...
- JAVA 中BIO,NIO,AIO的理解
[转自]http://qindongliang.iteye.com/blog/2018539 ?????????????????????在高性能的IO体系设计中,有几个名词概念常常会使我们感到迷惑不解 ...
- 36.在字符串中删除特定的字符[Delete source from dest]
[题目] 输入两个字符串,从第一字符串中删除第二个字符串中所有的字符.例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”. ...
- Linux C 单链表 读取文件 并排序 实例并解释
C的指针挺头疼的,先看一个例子: 给指针赋值和通过指针进行赋值这两种操作的差别确实让人费解.谨记区分的重要方法是:如果对左操作数进行解引用,则修改的是指针所指对象的值: 如果没有使用解引用操作, ...
- 【学习笔记】移动Web手册(PPK力作)
又是好久没写博客了,最近把近半年的总结,全部总结到博客园吧.先写最近的一个移动端的学习笔记.毕竟移动端开发了一段时间,就写一写读<移动web手册>中,对我感触比较深的几个点—— 一.浏览器 ...