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. 读取 XML 数据时,超出最大字符串内容长度配额 (8192)

    格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://www.thermo.com/informatics/xmlns/limswebservice 进行反序列化时出错: Process ...

  2. JSON.parse()和jQuery.parseJSON()的区别

    jQuery.parseJSON(jsonString) : 将格式完好的JSON字符串转为与之对应的JavaScript对象   (jquery 方法) 1 2 3 var str = '[{&qu ...

  3. [Android Pro] AtomicInteger的用法

    J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...

  4. 【BZOJ】【1006】【HNOI2008】神奇的国度

    弦图最小染色/MCS算法 Orz PoPoQQQ  (UPD:ydc的写法好像更熟悉一些……(类似堆优化的Dij啊~ 先留个坑……明天再看一看……感觉好神奇>_<(完美消除序列之于弦图 就 ...

  5. 使用DBCA工具创建自己的数据库

    ylbtech-Oracle:使用DBCA工具创建自己的数据库  DBCA创建数据库 默认安装的Oracle数据库一般不能满足实际应用的需求,例如数据库名称.数据库块的大小等都需要修改,那么我们应该自 ...

  6. C++获得本机所有网卡的IP和MAC地址信息

    一台机器上可能不只有一个网卡,但每一个网卡只有一个MAC地址,而每一个网卡可能配置有多个IP地址:如平常的笔记本电脑中,就会有无线网卡和有线网卡(网线接口)两种:因此,如果要获得本机所有网卡的IP和M ...

  7. Informatica 常用组件Expression之一 概述

            转换类型:被动.已连接 可以在写入目标前,使用表达式转换计算单行中的值.例如,您可能需要调整员工薪酬.连接姓名或将字符串转换为数字.您可以使用表达式转换执行任意非聚合计算.在将结果输出 ...

  8. Win Socket编程原理及简单实例

    [转]http://www.cnblogs.com/tornadomeet/archive/2012/04/11/2442140.html 使用Linux Socket做了小型的分布式,如Linux ...

  9. 《Linux总线、设备与驱动》USB设备发现机制

    说明:本分析基于mstar801平台Linux2.6.35.11内核,其他内核版本仅供参考. 一.程序在内核中的位置 1.usb host做为pci总线下的一个设备存在(嵌入式系统中有可能也会直接挂在 ...

  10. Objective-C-代码块Block回顾

    OC中的代码块是iOS4.0+ 和Mac OS X 10.6+ 引进的对C语言的扩展,用来实现匿名函数的特性.类似于其他语言脚本语言或者编程语言中的闭包或者是Lambda表达式,可能第一眼看上去很怪异 ...