题目链接:
题目描述:
Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

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
First line contains two integers stand for N and M.

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
For
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."
 
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
 
Sample Output
13
解题思路:
  刚开始以为BFS模板,后来发现拓展每一层后如果有士兵的话,需要花费两秒,造成该路径与同层拓展的路径的时间优先级不同了,自然就想到了使用优先队列进行搜索。
代码:
 #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+优先队列)的更多相关文章

  1. HDU 1242 Rescue(优先队列)

    题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description   Angel was caught by ...

  2. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  3. hdu 1242 Rescue (BFS)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. HDU 1242 rescue (优先队列模板题)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. HDU 1242——Rescue(优先队列)

    题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...

  6. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  7. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  8. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  9. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

随机推荐

  1. springmvc配置文件web.xml详解各方总结。

    Spring分为多个文件进行分别的配置,其中在servlet-name中如果没有指定init-param属性,那么系统自动寻找的spring配置文件为[servlet-name]-servlet.xm ...

  2. NET npoi帮助类

    nuget添加npoi /// <summary> /// npoi帮助类 /// </summary> public static class NpoiHelper { // ...

  3. winform 查找控件并获取特定类型控件

    //通过反射获取所有控件集合 System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection. ...

  4. C# 子线程调用主线程窗体的解决方法

    摘自其他人博客,自己试过确实解决问题.(如在自己定义的线程里面给textbox赋值) 由于Windows窗体控件本质上不是线程安全的.因此如果有两个或多个线程适度操作某一控件的状态(set value ...

  5. 重置identity值

    如果数据库在SQL2008或以上,可以参考下面方法: DBCC CHECKIDENT ( table_name [, { NORESEED | { RESEED [, new_reseed_value ...

  6. AngularJS指令封装高德地图组件

    1 概述 公司移动门户原来是基于AngularJS指令封装的百度地图组件,用于签到.签退.定位等功能,在使用过程中发现百度地图频繁的弹出广告,所以打算重新引用其它地图组件,最后决定基于AngularJ ...

  7. Android开发教程 - 使用Data Binding(二)集成与配置

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  8. 201621123018《Java程序设计》第9周学习报告

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 泛型个人认为可以理解为一种模糊的类型,在里面写入各种方法,程序员可以根据需要再创建具体类型的对象,然后调用泛型 ...

  9. [luogu 5301][bzoj 5503] [GXOI/GZOI2019] 宝牌一大堆

    题面 好像ZJOI也考了一道麻将, 这是要发扬中华民族的赌博传统吗??? 暴搜都不会打, 看到题目就自闭了, 考完出来之后看题解, \(dp\), 可惜自己想不出来... 对于国士无双(脑子中闪过了韩 ...

  10. .NET Core 常用加密和Hash工具NETCore.Encrypt

    前言 在日常开发过程中,不可避免的涉及到数据加密解密(Hash)操作,所以就有想法开发通用工具,NETCore.Encrypt就诞生了.目前NETCore.Encrypt只支持.NET Core ,工 ...