hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072
Description
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
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
Sample Input
Sample Output
#include<iostream>
#include<queue>
#include<cstdio> using namespace std; struct node{
int x,y;
int time;
int sss;
friend bool operator < (node a,node b){
return a.time > b.time;
}
}; int n,m,sx,sy,ex,ey;
int dir[][]={,,-,,,-,,};
int map[][]; bool inMap(int x,int y){
return x>=&&x<n&&y>=&&y<m;
} int bfs(){
node cur,next;
priority_queue<node> q;
cur.x = sx;
cur.y = sy;
cur.time = ;
cur.sss = ;
q.push(cur);
while(!q.empty()){
cur = q.top();
q.pop();
if(cur.sss<=)
return -;
if(cur.sss> && cur.x == ex && cur.y == ey)
return cur.time;
for(int i=;i<;i++){
int xx = cur.x + dir[i][];
int yy = cur.y + dir[i][];
if(inMap(xx,yy) && map[xx][yy]!= && cur.sss>){
next.sss = cur.sss-;
if(map[xx][yy]== && next.sss > ){
next.sss = ;
map[xx][yy] = ;
}
next.x = xx;
next.y = yy;
next.time = cur.time + ;
q.push(next);
}
}
}
return -;
} int main(){
int t,i,j;
cin>>t;
while(t--){
cin>>n>>m;
for(i=;i<n;i++)
for(j=;j<m;j++){
scanf("%d",&map[i][j]);
if(map[i][j]==)
sx=i,sy=j;
else if(map[i][j]==){
ex=i,ey=j;
map[i][j] = ;
}
}
cout<<bfs()<<endl;
}
return ;
}
hdu 1072 Nightmare (bfs+优先队列)的更多相关文章
- hdu - 1072 Nightmare(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- HDU 1072 Nightmare
Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...
- HDU 1072 Nightmare (广搜)
题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...
- HDU 1072 Nightmare 题解
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU 5040 Instrusive(BFS+优先队列)
题意比较啰嗦. 就是搜索加上一些特殊的条件,比如可以在原地不动,也就是在原地呆一秒,如果有监控也可以花3秒的时间走过去. 这种类型的题目还是比较常见的.以下代码b[i][j][x]表示格子i行j列在x ...
- HDU——1242Rescue(BFS+优先队列求点图最短路)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- Linux有问必答:如何在Linux中修改环境变量PATH
提问: 当我试着运行一个程序时,它提示“command not found”. 但这个程序就在/usr/local/bin下.我该如何添加/usr/local/bin到我的PATH变量下,这样我就可以 ...
- bzoj 3234: [Ahoi2013]立方体
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3234 题意:求长方体交的表面积. 思路:flood-fill const int ...
- Dungeon Master bfs
time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...
- pupper基线加固
1. 概述 puppet是一个开源的软件自动化配置和部署工具,它使用简单且功能强大,正得到了越来越多地关注,现在很多大型IT公司均在使用puppet对集群中的软件进行管理和部署,如google利用p ...
- SAP传输系统TMS、开发类、传输请求
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- POJ 3286 How many 0's?(几多0?)
POJ 3286 How many 0's?(几多0?) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A Benedi ...
- python内置函数的归集
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...
- 关于【bootstrap modal 模态框弹出瞬间消失的问题】
前提是你没有重复引入bootstrap.js\bootstrap.min.js和modal.js.一下提供一个小例子. <button class="btnbtn-primary bt ...
- Spring的DI(Ioc) - 利用构造器注入
1: 在给对象提供构造器 public class PersonServiceImpl implements PersonService { private PersonDao personDao; ...
- Spring的DI(Ioc) - 注入bean 和 基本数据类型
注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...