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. C++学习笔记-2-构造函数和析构函数

    问题2. 什么时候执行构造函数和析构函数  22:59:40 2015-07-22 做了一个实验: #include <iostream> class Object{ public: Ob ...

  2. 用3种方法在 operator= 中处理“自我赋值”

    假设你建立一个class 用来保存一个指针指向一块动态分配的位图. class Bitmap {......}; class Widget{ ... private: Bitmap* pb ; }; ...

  3. ACM HDU 2044 一只小蜜蜂

    Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input 输入数据的第一 ...

  4. tomcat gc问题总结

    Java内存泄露监控工具:JVM监控工具介绍  http://developer.51cto.com/art/201203/321431.htm 关于施用full gc频繁的分析及解决  http:/ ...

  5. iphone5手机端内容超出iphone6没问题且超出内容为http://.....网址

  6. mysql备份和恢复

    摘自:http://safe.it168.com/a2009/1108/805/000000805490.shtml 要备份数据库" phpbb_db_backup " #mysq ...

  7. ecshop安装程序源码阅读-安装脚本(2)

    检查环境变量: 程序目录:图片目录,数据目录,临时目录 模板目录下模板文件 数据库连接函数 数据库配置: 读取数据库列表 创建配置文件(数据库,语言,session有效期等) 创建数据表 创建初始化数 ...

  8. HttpWebRequest 写入报错

    HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size ...

  9. File System Shell

    Overview appendToFile cat chgrp chmod chown copyFromLocal copyToLocal count cp du dus expunge get ge ...

  10. boost之algorithm/string

    头文件: #include<iostream>#include <boost/algorithm/string.hpp>using namespace std;using na ...