HDU 1242 Rescue(BFS+优先队列)
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.)
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.
each test case, your program should output a single integer, standing
for the minimal time needed. If such a number does no exist, you should
output a line containing "Poor ANGEL has to stay in the prison all his
life."
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; struct node{
int x,y,s;
bool operator < (const node &a) const {
return a.s<s;};
};
bool bk[][];
char map[][];
void bfs(int sx,int sy);
int n,m,ex,ey; int main()
{
//freopen("E:\\testin.txt","r",stdin);
int sx,sy;
while(scanf("%d%d",&n,&m) != EOF){
memset(map,,sizeof(map)); for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf(" %c",&map[i][j]);
if(map[i][j] == 'r')
{
sx=i;sy=j;
}
}
} bfs(sx,sy);
}
return ;
} void bfs(int sx,int sy){
priority_queue<struct node> q;
struct node head,temp,temp1;
memset(bk,,sizeof(bk));
head.x=sx;
head.y=sy;
head.s=;
int next[][]={-,,,,,,,-};
q.push(head);
bk[sx][sy]=;
int flag=,tx,ty; while(!q.empty()){
temp=q.top();q.pop(); for(int i=;i<;i++){
tx=temp.x+next[i][];
ty=temp.y+next[i][]; if(tx < || tx > n || ty < || ty > m || map[tx][ty] == '#' || map[tx][ty] == 'r')
continue;
if(bk[tx][ty] == ){
bk[tx][ty] = ; if(map[tx][ty] == '.'){
temp1.x=tx;
temp1.y=ty;
temp1.s=temp.s+;
q.push(temp1);
}
if(map[tx][ty] == 'x'){
temp1.x=tx;
temp1.y=ty;
temp1.s=temp.s+;
q.push(temp1);
}
if(map[tx][ty] == 'a'){
flag=;
printf("%d\n",temp.s+);
}
}
}
}
if(flag == )
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
HDU 1242 Rescue(BFS+优先队列)的更多相关文章
- HDU 1242 Rescue(优先队列)
题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by ...
- 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 1242 rescue (优先队列模板题)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1242——Rescue(优先队列)
题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
随机推荐
- [chrome-debug]如何使用chrome调试你的移动设备
### usb debug by remote chrome ### https://developer.chrome.com/devtools/docs/remote-debugging ### c ...
- mysql 唯一索引UNIQUE使用方法详解
创建唯一索引的目的不是为了提高访问速度,而只是为了避免数据出现重复.唯一索引可以有多个但索引列的值必须唯一,索引列的值允许有空值.如果能确定某个数据列将只包含彼此各不相同的值,在为这个数据列创建索引的 ...
- [leetcode 14]Longest Common Prfix
1 题目: Write a function to find the longest common prefix string amongst an array of strings. Hide Ta ...
- 一致性hash(整理版)
简单解释: 简单解释一致性hash的原理:网上通篇都是用服务器做的举例,我这里也如此,主要是便于理解. 通常:有N个客户端请求服务器,假设有M台web服务器,通常为了均衡访问会进行N%M的取模,然后分 ...
- SQL Server 字符串拼接、读取
一.查询结果使用,字符串拼接 declare @names nvarchar(1000) declare @ParmDefinition nvarchar(1000) declare @sqltext ...
- 关于错误 openssl/ssl.h:没有那个文件或目录的解决办法
原文链接:https://blog.csdn.net/kulala082/article/details/68484314 首先得安装openssl:sudo apt-get install open ...
- 人工智能-机器学习之seaborn(读取xlsx文件,小提琴图)
我们不止可以读取数据库的内容,还可以读取xlsx文件的内容,这个库有在有些情况还是挺实用的 首先我们想读取这个文件的时候必须得现有个seaborn库 下载命令就是: pip install seab ...
- SpringBoot从入门到逆天(1)
1.SpringBoot是什么? <1>为Sping开发提供一个更 快捷更广泛的入门体验. <2>开箱即用,不合适时特可以快速抛弃. <3>提供一系列大型项目常用的 ...
- 理解WSGI
WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义 ...
- Python数据结构:栈 队列
栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIFO)的特征 2.队列(queue)是一种具有 ...