来源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. 常见问题1:默认div隐藏,点击按钮时出现,再点击时隐藏。

    例:默认div隐藏,点击按钮时出现,再点击时隐藏. <a href="#" onclick="f('ycbc')"; >控制按钮</a> ...

  2. VBV Rate Control

    Part 1 <06/05/07 12:08pm> Manao | he is negating a float by printing it, adding a "-" ...

  3. jPlayer获取播放时间

    关于jPlayer的用法,可以参考:jPlayer 2.6.0开发者手册 http://www.jplayer.cn/developer-guide.html 视频播放例子: //视频播放 var v ...

  4. [Web 前端] mobx教程(五)-Mobx常见问题及解决方案(1)Mobx使用严格模式

    copy from : https://blog.csdn.net/smk108/article/details/83185745 mobx在严格模式下,不允许在 action 外更改任何状态.但是不 ...

  5. Elasticsearch集群优化

    版本配置: ES版本:6.2.4 OS内存64G. 一.参数配置: ES jvm内存31G. vi /etc/sysctl.conf vm.swappiness = 1 elasticsearch.y ...

  6. 简单理解 RPC(转载)

    原文地址:http://www.syyong.com/architecture/Simple-understanding-of-RPC.html RPC(Remote Procedure Call P ...

  7. Unity应用架构设计(1)—— MVVM 模式的设计和实施(Part 1)

    初识 MVVM 谈起 MVVM 设计模式,可能第一映像你会想到 WPF/Sliverlight,他们提供了的数据绑定(Data Binding),命令(Command)等功能,这让 MVVM 模式得到 ...

  8. Effective Java 第三版——67. 明智谨慎地进行优化

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  9. 使用ansible结合FTP部署zabbix_agent

    想要达到的效果:一条命令,快速对多台主机部署zabbix_agent 实现思路:从源码编译编译出zabbix_agentd,准备好安装脚本,将安装脚本和编译出的agent一起上传到FTP服务器,在an ...

  10. 各常用分类算法的优缺点总结:DT/ANN/KNN/SVM/GA/Bayes/Adaboosting/Rocchio

    1决策树(Decision Trees)的优缺点 决策树的优点: 一. 决策树易于理解和解释.人们在通过解释后都有能力去理解决策树所表达的意义. 二. 对于决策树,数据的准备往往是简单或者是不必要的. ...