hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 14 Accepted Submission(s) : 7
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
Author
Source
BFS广搜 + 优先队列。
题意:从前有一名天使被囚禁了,天使的朋友要去救他,你的任务是输出最短的救援时间。天使的朋友每秒可以走一步,地牢中有守卫,当你遇到守卫的时候需要停留一秒杀死守卫。给你地牢的地图,上面有几种元素,'.'表示路,可以通行。'#'代表墙,无法通行。'r'表示天使的朋友,代表起点。'a'表示天使的位置,代表终点。'x'表示守卫的位置。
思路:因为是求“最短路径”的问题,正好用广搜可以解决。但是与普通广搜不同的是,遇到守卫的时候会多停留一秒。这就会导致队列中优先级的不稳定,所以需要用优先队列,让优先级最高的节点始终在队列最前面。
代码:
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
char a[][];
bool isv[][]; //记录访问过没有
int dx[] = {,,,-};
int dy[] = {,,-,};
int N,M;
int sx,sy,ex,ey;
struct NODE{
int x;
int y;
int step;
friend bool operator < (NODE n1,NODE n2) //自定义优先级。在优先队列中,优先级高的元素先出队列。
{
return n1.step > n2.step; //通过题意可知 step 小的优先级高,需要先出队。
}
};
bool judge(int x,int y)
{
if( x< || y< || x>N || y>M )
return ;
if( isv[x][y] )
return ;
if( a[x][y]=='#' )
return ;
return ;
}
int bfs() //返回从(x,y)开始广搜,到右下角的最短步数,如果无法到达右下角,返回0
{
memset(isv,,sizeof(isv));
priority_queue <NODE> q; //定义一个优先队列
NODE cur,next;
cur.x = sx;
cur.y = sy;
cur.step = ;
isv[sx][sy] = true;
q.push(cur); //第一个元素入队
while(!q.empty()){
cur = q.top(); //队首出队,注意不是front()
q.pop();
if(cur.x==ex && cur.y==ey) //到终点
return cur.step;
for(int i=;i<;i++){
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if( judge(nx,ny) ) //判定
continue;
//可以走
next.x = nx;
next.y = ny;
if(a[nx][ny]=='x')
next.step = cur.step + ;
else
next.step = cur.step + ;
isv[nx][ny] = true;
q.push(next);
}
}
return -;
}
int main()
{
while(cin>>N>>M){
for(int i=;i<=N;i++) //输入
for(int j=;j<=M;j++){
cin>>a[i][j];
if(a[i][j]=='a')
ex=i,ey=j;
else if(a[i][j]=='r')
sx=i,sy=j;
}
int step = bfs();
if(step==-) //不能到达
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else
cout<<step<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1242:Rescue(BFS广搜 + 优先队列)的更多相关文章
- HDU 1242 Rescue (广搜)
题目链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The priso ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- hdu 1242 Rescue (BFS)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU - 3345 War Chess 广搜+优先队列
War chess is hh's favorite game: In this game, there is an N * M battle map, and every player has hi ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1180:诡异的楼梯(BFS广搜)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- BFS广搜题目(转载)
BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...
随机推荐
- JavaScript的apply和call方法及其区别
参考资料: http://blog.csdn.net/myhahaxiao/article/details/6952321 apply和call能“劫持”其他对象的方法来执行,其形参如下: apply ...
- DataGridView设置不自动显示数据库中未绑定的列
项目中将从数据库查出来的数据绑定到DataGridView,但是不想显示所有的字段.此功能可以通过sql语句控制查出来的字段数目,但是DataGridView有属性可以控制不显示未绑定的数据,从UI层 ...
- iOS-解决iOS8及以上设置applicationIconBadgeNumber报错的问题
在IOS7中设置applicationIconBadgeNumber不会有什么问题,但是直接在IOS8中设置applicationIconBadgeNumber会报错.因为在IOS8中要想设置appl ...
- WIN7下使用VC2010调试uCOS-II 2.91
WIN7下使用VC2010调试uCOS-II 2.91 http://www.amobbs.com/thread-5462878-1-1.html ucos系统学习汇总 http://www.cnbl ...
- C++ 迭代器 基础介绍
C++ 迭代器 基础介绍 迭代器提供对一个容器中的对象的访问方法,并且定义了容器中对象的范围.迭代器就如同一个指针.事实上,C++的指针也是一种迭代器.但是,迭代器不仅仅是指针,因此你不能认为他们一定 ...
- C++中构造函数详解及显式调用构造函数
C++构造函数详解及显式调用构造函数 c++类的构造函数详解 一. 构造函 ...
- [LA4108]SKYLINE
[LA4108]SKYLINE 试题描述 The skyline of Singapore as viewed from the Marina Promenade (shown on the left ...
- [BZOJ1998][Hnoi2010]Fsk物品调度
[BZOJ1998][Hnoi2010]Fsk物品调度 试题描述 现在找工作不容易,Lostmonkey费了好大劲才得到fsk公司基层流水线操作员的职位.流水线上有n个位置,从0到n-1依次编号,一开 ...
- angular js 自定义指令
我们有些时候需要把后台返回过来的带有html标签的字符串binding到界面中一个指定的div或者其他的控制器中. 使用普通ng-bind不会自动解析出html语句. js中这样定义: app.dir ...
- POJ 1062 昂贵的聘礼
C - 昂贵的聘礼 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit St ...