来源poj

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

和之前坦克大战一模一样

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=3e2+10;
char MAP[N][N];
struct node
{
int x,y,ans;
friend bool operator <(node a,node b){ return a.ans>b.ans; }
node(int a=0,int b=0){ x=a;y=b;ans=0; }
};
int a[4][2]={-1,0,1,0,0,1,0,-1};
priority_queue<node>q;
bool checkt(node a)
{
return (MAP[a.x][a.y]=='a');
}
int visit[N][N];
int bfs()
{
node t,tt;
while(!q.empty())
{
t=q.top();
q.pop();
visit[t.x][t.y]=1;
rep(i,0,4)
{
tt.ans=t.ans+1;
tt.x=t.x+a[i][0];
tt.y=t.y+a[i][1];
if(!visit[tt.x][tt.y])
{
if(checkt(tt))
return tt.ans;
if(MAP[tt.x][tt.y]=='.')
q.push(tt);
else if(MAP[tt.x][tt.y]=='x')
{
tt.ans++;
q.push(tt);
}
visit[tt.x][tt.y]=1;
}
}
}
return -1;
}
void display(int n,int m)
{
mm(visit,0);
while(!q.empty()) q.pop();
int x,y;
rep(i,1,n+1)
rep(j,1,m+1)
if(MAP[i][j]=='r')
{ x=i;y=j;}
q.push(node(x,y));
int ans=bfs();
if(ans==-1)
pf("Poor ANGEL has to stay in the prison all his life.\n");
else
prf(ans);
}
int main()
{
int n,m;
while(~scff(n,m)&&n&&m)
{
mm(MAP,'#');
rep(i,1,n+1)
{
sf("%s",MAP[i]+1);
MAP[i][m+1]='#';
}
display(n,m);
}
return 0;
}

F - Rescue 优先队列bfs的更多相关文章

  1. ZOJ 649 Rescue(优先队列+bfs)

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

  2. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  3. 【POJ3635】Full Tank 优先队列BFS

    普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...

  4. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  5. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

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

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

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

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

  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,优先队列,基础)

    题目 /******************以下思路来自百度菜鸟的程序人生*********************/ bfs即可,可能有多个’r’,而’a’只有一个,从’a’开始搜,找到的第一个’r ...

随机推荐

  1. 如何在MyBatis中优雅的使用枚举

    问题 在编码过程中,经常会遇到用某个数值来表示某种状态.类型或者阶段的情况,比如有这样一个枚举:   public enum ComputerState { OPEN(10), //开启 CLOSE( ...

  2. Unity3D性能优化最佳实践(四)资源审查

    Asset auditing - 资源审查 许多项目发生效能问题的真正原因只是由于人员操作不当或是试东试西,而不小心改到导入设定影响到导入的资源.(例如最近的gitlab惨案) 对于较大规模的项目,最 ...

  3. VuePress

    VuePress 这篇文章主要是记录自己在使用VuePress过程中所遇到的问题以及如何一步一步的解决问题. 安装vuepress前,请确保你的 Node.js 版本 >= 8 全局安装 # 安 ...

  4. IntelliJ IDEA 下载安装(含注册码)

    https://blog.csdn.net/mashuai720/article/details/79389314

  5. Window下使用Charles对手机的Https请求进行抓包

    https://blog.csdn.net/zhaoerduo/article/details/52128607

  6. SSE图像算法优化系列二十四: 基于形态学的图像后期抗锯齿算法--MLAA优化研究。

    偶尔看到这样的一个算法,觉得还是蛮有意思的,花了将近10天多的时间研究了下相关代码. 以下为百度的结果:MLAA全称Morphological Antialiasing,意为形态抗锯齿是AMD推出的完 ...

  7. 【ASP.NET Core】EF Core 模型与数据库的创建

    大家好,欢迎收看由土星卫视直播的大型综艺节目——老周吹逼逼. 今天咱们吹一下 EF Core 有关的话题.先说说模型和数据库是怎么建起来的,说装逼一点,就是我们常说的 “code first”.就是你 ...

  8. bcrypt 加密

    关于 bcrypt:1.bcrypt是不可逆的加密算法,无法通过解密密文得到明文.2.bcrypt和其他对称或非对称加密方式不同的是,不是直接解密得到明文,也不是二次加密比较密文,而是把明文和存储的密 ...

  9. 只有设置了 name 属性的表单元素才能在提交表单时传递它们的值

    $(function () { var wait = $("<img src=\"\" alt=\"正在上传\"/>"); $( ...

  10. 设置DataGridView不自动创建生成列

    DataGridView.AutoGenerateColumns 获取或设置一个值,该值指示是否为数据源中的每一字段自动创建 BoundColumn 对象并在 DataGrid 控件中显示这些对象. ...