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做肯定出 ...
随机推荐
- [poj2104] K-th Number (主席树)
主席树 Description You are working for Macrohard company in data structures department. After failing y ...
- 进程间通信之打开关闭一个exe文件
一.打开 1.WinExec("F:\\QQ\\Bin\\QQ.exe",SW_SHOW); 注意:'\'需要改成'\\',阻塞 2. STARTUPINFO si; PROCES ...
- SqlSever基础 Upper函数 返回字符串的大写形式
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- BZOJ 3640 JC的小苹果(逆矩阵)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3640 题意:给出一个无向图,从1走到n.开始是血量H,从u到达v时血量减少a[v] ...
- window删除损坏无法打开的文件
移动硬盘删除文件时提示“文件或目录损坏且无法读取”的解决方法-chkdsk 命令的巧用 新买一个移动硬盘,同学借去Copy一个游戏,拷来后发现数据包损坏,提示"文件或目录损坏且无法读取&qu ...
- DevExpress所有的窗体,使用同一款皮肤
https://www.devexpress.com/Support/Center/Question/Details/K18516 To accomplish your task, please ex ...
- FJNU 1153 Fat Brother And XOR(胖哥与异或)
FJNU 1153 Fat Brother And XOR(胖哥与异或) Time Limit: 1000MS Memory Limit: 257792K [Description] [题目描述] ...
- Xcode error: conflicting types for 'XXXX'
问题描述:在main方法中调用了一个写在main方法后面的方法,比如: void main(){ A(); } void A(){} Xcode编译后就报错:conflicting types for ...
- Xcode:Foundation框架找不到,或者是自动提示出现问题
问题描述:Foundation框架找不到,或者是自动提示出现问题 之前的操作:手贱,不少心把编译器里面的源码改了 处理办法:清理缓存 缓存位置:点击桌面后,选择系统菜单栏:前往—电脑—硬盘—用户—ap ...
- 禁止 IOS 系统 数字 变超链 (自动识别为电话号码)
在测试中发现iPad上的Safari总会把长串数字识别为电话号码,文字变成蓝色,点击还会弹出菜单添加到通讯录. 别的地方倒也罢了,如果在用户名中出现数字(手机注册新浪微博的话用户名就是“手机用户xxx ...