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做肯定出 ...
随机推荐
- BZOJ 2566 xmastree(树分治+multiset)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2566 题意:一棵有边权的树.结点有颜色.每次修改一个点的颜色.求每次修改后所有同色 ...
- 2012 #3 Arcane Numbers
Arcane Numbers 1 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- C#中Struct与Class的区别
class和struct最本质的区别是class是引用类型,而struct是值类型,它们在内存中的分配情况有所区别. 什么是class? class(类)是面向对象编程的基本概念,是一种自定义数据结构 ...
- [转]Unity3D:Gizmos画圆(原创)
using UnityEngine; using System; public class HeGizmosCircle : MonoBehaviour { public Transform m_Tr ...
- 借助Nodejs在服务端使用jQuery采集17173游戏排行信息
Nodejs相关依赖模块介绍 Nodejs的优势这里就不做介绍啦,这年头相信大家对它也不陌生了.这里主要介绍一下用到的第三方模块. async:js代码中到处都是异步回调,很多时候我们需要做同步处理, ...
- Using GET_GROUP_SELECTION For Record Groups in Oracle Forms
Retrieves the sequence number of the selected row for the given group. Suppose you want to get a par ...
- Beaglebone Black–智能家居控制系统 LAS - 用 UART 连接 ESP8266 (ESP-01 版)
这是一块便宜 (¥12.5)的 WiFi 模块,3.3V ,芯片是乐鑫科技(Espressif)出品.它本身是很多玩法,比如这个 NodeMCU (淘宝有套件焊接好一整套的带 USB 接口的,搜 es ...
- JS——ajax login test
1.新建一个webproject,我用的是myeclipse10,建立如下的LoginServlet.java文件 2.编写java文件 import java.io.IOException; imp ...
- Python基础学习笔记(十三)异常
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-exceptions.html Python用异常对象(excep ...
- 不要温柔地走入AMD
1.无依赖情况 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...