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. Solution Explorer 和 Source Control Explorer 的 View History 异同

    如果查看别人对代码的修改,你可能会非常烦恼与在 Solution Explorer 中看历史版本看不全,如下: 实际上,你想看到的是对于整个解决方案,全部的历史版本,那应该跑去 Source Cont ...

  2. 缓存算法:LRU、LFU、FIFO

      LRU全称是Least Recently Used,即最近最久未使用的意思.如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小.也就是说,当限定的空间已存满数据时,应当把最久 ...

  3. 【BZOJ】【3240】【NOI2013】矩阵游戏

    十进制快速幂+矩阵乘法+常数优化 听说这题还可以强行算出来递推式……然后乘乘除除算出来…… 然而蒟蒻选择了一个比较暴力的做法= = 我们发现这个递推的过程是线性的,所以可以用矩阵乘法来表示,$x=a* ...

  4. ubuntu下mongodb常用命令

    1. 启动脚本 #!/bin/bash mongod --dbpath /usr/local/mongodb/data1 chmod +x run-mongodb 2. 关闭数据库服务 官方文档说可以 ...

  5. Spark一个简单案例

    Spark是一个类似Map-Reduce的集群计算框架,用于快速进行数据分析. 在这个应用中,我们以统计包含"the"字符的行数为案例,.为建立这个应用,我们使用 Spark 1. ...

  6. Command 命令模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. Jquery中parent()和parents()

    一.parent()方法 此方法取得匹配元素集合中每个元素的紧邻父元素,也就是第一级父元素,而不是所有的祖先元素.所取得的父元素集合也可以使用表达式进行筛选. 二.parents()方法 此方法取得一 ...

  8. 5. How to set up a Activity

    1. Create a new xml in "layout" folder "splah.xml" <?xml version="1.0&qu ...

  9. Highstock生成股票K线图

    在线演示 本地下载 使用HightStock生成股票K线图例子.

  10. ThinkPHP3.2 新bug ReadHtmlCache 支持不区分大写和小写的函数

    报错提示: Fatal error: Function name must be a string in D:\wwwroot\zbphp.com\ThinkPHP\Library\Behavior\ ...