Nightmare

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8480    Accepted Submission(s):
4069

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
看完题发现这个题以前做过  然而我还是厚颜无耻的把它贴出来了;
题意:2为起点3为终点,0不可以过1可以过,遇到4可以更新时间为6,但是一个4只能用一次,走一步浪费一秒钟,
        如果六秒用完还没走出迷宫或者还没遇到4则走不出来了输出-1否则输出步数
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int map[10][10];
int n,m;
int x1,y1,x2,y2;
struct node
{
int x,y;
int time;
int step;
friend bool operator < (node a,node b)
{
return a.step>b.step;
}
};
void bfs()
{
int i,j;
priority_queue<node>q;
int move[4][2]={0,1,0,-1,-1,0,1,0};
node beg,end;
beg.x=x1;
beg.y=y1;
beg.time=6;
beg.step=0;
q.push(beg);
while(!q.empty())
{
end=q.top();
q.pop();
if(end.x==x2&&end.y==y2)
{
printf("%d\n",end.step);
return ;
}
if(end.time==1)
continue;
for(i=0;i<4;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
if(beg.x>=0&&beg.x<n&&beg.y>=0&&beg.y<m&&map[beg.x][beg.y]!=0)
{
if(map[beg.x][beg.y]==4)
{
beg.time=6;
map[beg.x][beg.y]=1;
}
else
beg.time=end.time-1;
beg.step=end.step+1;
q.push(beg);
}
}
}
printf("-1\n");
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==2)
{
x1=i;y1=j;
}
else if(map[i][j]==3)
{
x2=i;y2=j;
}
}
}
bfs();
}
return 0;
}

  

hdoj 1072 Nightmare的更多相关文章

  1. 【HDOJ】1072 Nightmare

    bfs,使用ttl进行剪枝. #include <iostream> #include <cstdio> #include <cstring> #include & ...

  2. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

  3. HDU 1072 Nightmare

    Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...

  4. HDU 1072 Nightmare (广搜)

    题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...

  5. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  6. HDU 1072 Nightmare 题解

    Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  7. BFS(双向) HDOJ 3085 Nightmare Ⅱ

    题目传送门 题意:一个人去救女朋友,两个人都在运动,还有鬼在"扩散",问最少几秒救到女朋友 分析:开两个队列来表示两个人走过的路,一个人走到的地方另一个人已经vis了,那么就是相遇 ...

  8. 步步为营(十六)搜索(二)BFS 广度优先搜索

    上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...

  9. HDU 1072 (不一样的入队条件) Nightmare

    之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们 ...

随机推荐

  1. Page.ClientScript.RegisterStartupScript不执行问题

    c#后台使用Page.ClientScript.RegisterStartupScript在前台注册一段脚本提示,发现没有效果,寻寻觅觅,终于从度娘处找到了原因: 该页面多次使用到了Page.Clie ...

  2. C++判断Office版本——转载

    自:http://blog.csdn.net/lpc_china/article/details/18359145 主要原理:查询windows注册表microsoft office软件项的值来判断版 ...

  3. wamp不能使用phpmyadmin,提示“You don't have permission to access /phpmyadmin/ on this server.” 转载

    换了win8之后wamp明显不怎么好用了,显示80端口被system占用,后是masql出现了403错误,多番百度谷歌找到了解决方案,这里与大家分享 当你安装完成wamp后,打开localhost或i ...

  4. [lua]再版jobSchedule与脚本描述范型

    首先贴上代码 -- CPM:关键路径法(Critical Path Method) jobSchedule = { todos = { -- todo list ... ["finale&q ...

  5. 【转】oracle数据库NUMBER数据类型

    原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定prec ...

  6. 大数据技术人年度盛事! BDTC 2016将于12月8-10日在京举行

    2016年12月8日-10日,由中国计算机学会(CCF)主办,CCF大数据专家委员会承办,中国科学院计算技术研究所和CSDN共同协办的2016中国大数据技术大会(Big Data Technology ...

  7. JavaScript学习心得(五)

    一时间 1970年1月1日是电脑常用的时间参考点,称为纪元(Epoch)或者UNIX时间戳(UNIX Epoch).JavaScript中的Date对象能够表示1970年1月1日子夜前后1亿天之内的任 ...

  8. Android 开机过程PMS分析

    12-13 12:25:05.595 3253 3253 SystemServer !@Boot: Start PackageManagerService pm/PackageManagerServi ...

  9. iOS: performSelectorOnMainThread(译)

    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)waitperfor ...

  10. 小A老空调需求管理小记

    1. 关注一个动作的流程和内容 比如登录,我需要提供的是登录接口,但是登录是否只是一个动作?一个动作一定不完整,还应该包括动作的内容和步骤(流程),这里有两个问题:就是落地和把问题想复杂了:其实登录还 ...