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. List 集合转换为String

    开发中会用到把 List<string>  的内容拼接成以逗号分隔的字符串的形式,现总结如下: 方法一: public String listToString(List list, cha ...

  2. #line 的作用是改变当前行数和文件名称

    #line 的作用是改变当前行数和文件名称,它们是在编译程序中预先定义的标识符命令的基本形式如下:   #line number["filename"]其中[]内的文件名可以省略. ...

  3. 在JSP中应用JavaBean

    1. 解决中文乱码的JavaBean 在JSP页面中,处理中文字符经常会出现字符乱码的现象,特别是通过表单传递中文数据时容易产生.它的解决办法有很多,如将request的字符集指定为中文字符集,编写J ...

  4. 给定任意字符串,计算一共能组合成多少个单词bing

    CSDN编程挑战里的题目 例如有一个字符串"iinbinbing",截取不同位置的字符‘b’.‘i’.‘n’.‘g’组合成单词"bing".若从1开始计数的话, ...

  5. 3维DEMO: 抽奖圆盘

    抽奖圆盘 前些日子去超市,消费满一定钱数可以参加抽奖,就是在电视机上有个可旋转的圆盘,按一键开始,按一键抽奖结束.看到最大奖的扇形区域大约有个10度角的样子,按说中大奖的概率应该是36分之1.当然,这 ...

  6. ransom-note

    https://leetcode.com/problems/ransom-note/ public class Solution { public boolean canConstruct(Strin ...

  7. Chain of Responsibility 责任链模式 MD

    责任链模式 简介 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链,请求在这个链上[传递],直到链上的某一个对象决定处理此请求.发出这个请求的客户 ...

  8. Java 解析Excel文件为JSON

    Excel转Json的需求 反正我对SSM基本不会的情况下来到现在这家公司,都是90后,感觉很好.第二天就给我开发任务,就是把用户上传的Excel文件转成JSON返回给前台用于大屏的数据展示. 解决方 ...

  9. TextBox_TextChanged

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { TextBox textBox = sender a ...

  10. zypper命令使用示例

    导读 Zypper是OpenSUSE和企业版SUSE中软件包管理器ZYpp的命令行接口. 主要用于:1.管理软件包:zypper可用来安装.删除.更新和查询本地或远程的软件包.2.管理仓库:zyppe ...