Nightmare

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

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 的应用范围:
求最值问题.....属于盲目搜索的一种....
特点是所用的内存大相对于dfs....
代码如下:
 #include<cstdio>
#include<iostream>
#include<deque>
#define maxn 10
#define SET 6
using namespace std; int map[maxn][maxn],nn,mm;
int dir[][]=
{
{,}, /*向右*/
{,-}, /*向左*/
{-,}, /*向下*/
{,} /*向上*/
} ;
struct node{
int x,y; /*记录位置*/
int ans,time; /*步数和时间*/
}start; /*生成地图*/
void save_map()
{
for(int i=;i<nn;i++)
{
for(int j=;j<mm;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==)
{
start.x=i;
start.y=j;
start.ans=;
start.time=SET; /*设定为6s*/
}
}
}
return ;
} void bfs( void )
{
deque<node>q ; /*队列实现*/ node q1,q2; //暂存
q.push_back(start); //将start的位置初始化..... /* 当队列不为空的时候,执行下列程序 */
while(!q.empty())
{
q1=q.front(); //将对头的数据拿出来--->q1;
q.pop_front(); //q.pop()一样
for(int i=; i< ;i++)
{
/*进入下一个状态*/
q2.x=q1.x+dir[i][];
q2.y=q1.y+dir[i][];
q2.ans=q1.ans+;
q2.time=q1.time-;
//如果满足这些条件便可进行下一步搜索
if(q2.x>=&&q2.y>=&&q2.x<nn&&q2.y<=mm&&map[q2.x][q2.y]!=&&q2.time>)
{
/*说明找到了答案,可以结束了*/
if(map[q2.x][q2.y]==)
{
printf("%d\n",q2.ans); return ;
}
else if(map[q2.x][q2.y]==)
{
/*重置时间,其他照常*/
q2.time=SET;
map[q2.x][q2.y]=; //走过,不可再走,不然没完没了
}
q.push_back(q2);
}
}
}
/*说明不存在*/
printf("-1\n");
return ;
} int main()
{
int test;
scanf("%d",&test);
while(test--)
{
scanf("%d %d",&nn,&mm);
save_map();
bfs();
}
return ;
}

HDUOJ-----(1072)Nightmare(bfs)的更多相关文章

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

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

  2. hdu - 1072 Nightmare(bfs)

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

  3. HDU 1072 Nightmare

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

  4. hdoj 1072 Nightmare

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

  5. HDU 1072 Nightmare (广搜)

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

  6. HDU 1072 Nightmare 题解

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

  7. Nightmare BFS

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

  8. 【HDOJ】1072 Nightmare

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

  9. hdoj1072 Nightmare bfs

    题意:在一个地图里逃亡,2是起点,3是终点,1是路,0是墙,逃亡者携带一个炸弹,6分钟就会炸,要在6分钟前到达4可以重制时间,问是否能逃亡,若能则输出最小值 我的思路:bfs在5步内是否存在3,存在则 ...

随机推荐

  1. [MAC OS] 解压Assets.car获取资源图片

    reference to  : http://www.jianshu.com/p/a5dd75102467 今天想获取APP的资源,但是查看xxx.app文件夹里面,缺少了大部分资源.在文件夹里面发现 ...

  2. Objective-C:三种文件导入的方式比较

    三种文件导入的方式比较:   类的前项声明@class.import.include:   1.采用@class 类名的方式,它会告诉编译器有这么一个类,目前不需要知道它内部的实例变量和方法是如何定义 ...

  3. Objective-C:浅复制(拷贝)

    浅复制:复制对象时,如果对象中包含对象类型的实例变量,只是复制指针.新对象中的对象类型实例变量和旧对象中的对象类型实例变量指的是同一个对象.任何一方实例变量对对象做修改,另一方实例变量指向的该对象也就 ...

  4. jQuery的deferred对象使用笔记

    一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组),它们 ...

  5. 简单JavaScript语句实现搜索关键字高亮功能

    高亮功能主要是指对页面中指定区域的指定文字进行高亮显示,也就是背景着色.一般在搜索结果页面会经常用到这个功能. 下面就为大家提供一种解决方案,用javascript实现. 首先在<head> ...

  6. 《iOS Human Interface Guidelines》——Search Bar

    搜索栏 搜索栏接收用户输入用于搜索的文本(例如以下,带有占位文本). API NOTE 查看UISearchBar学习怎样在你的代码中定义搜索栏.查看UISearchDisplayController ...

  7. Redis自学笔记 --string类型

    string类型                                                                                  set 赋值 get ...

  8. word 文档如何加密

    给Word文档加密主要有以下几个方法:文件加密文件菜单设置:1.打开需要加密的Word文档.2.选“文件”的“另存为”,出现“另存为”对话框,在“工具”中选“常规选项”,出现“保存”选项卡.3.分别在 ...

  9. 重新安装 RCU-数据库 2014-11-22

    删除数据库Endv(原RCU数据库) 重建数据库为LLS(新RCU数据库)..略.. Database Control URL 为 https://www:1158/em 管理资料档案库已置于安全模式 ...

  10. [React] Simplify and Convert a Traditional React Form to Formik

    Forms in React are not easy. T render() { return ( <React.Fragment> <h2>Regular Maintena ...