来源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. .Net转Java.02.数据类型

    .NET中常见的数据类型分类分别是值类型和引用类型 值类型包括(基元类型.struct.枚举) 引用类型包括(类.类.数组.接口.指针) Java分为,基本类型和类   C#   Java   值类型 ...

  2. 开源MSSQL Express Profile 文件

    https://files.cnblogs.com/files/mqingqing123/ExpressProfile.rar

  3. spring mvc \ spring boot 允许跨域请求 配置类

    用@Component 注释下,随便放个地方就可以了 package com.chinaws.wsarchivesserver.core.config; import org.springframew ...

  4. angualrjs 配置超时时间

    timeout 1 本想通过$httpProvider的defaults属性配置timeout时间, defaults中没有这个属性. https://docs.angularjs.org/api/n ...

  5. Google I/O 官方应用中的动效设计

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jILRvRTrc/article/details/82881743 作者:Nick Butcher, ...

  6. atitit r9 doc on home ntpc .docx

    卷 p2soft 的文件夹 PATH 列表 卷序列号为 9AD0-D3C8 D:. │  Aittit pato 面对拒绝  的回应.docx │  Atitit  中国明星数量统计 attilax. ...

  7. Java8新特性interface中的static方法和default方法

    static方法 java8中为接口新增了一项功能:定义一个或者更多个静态方法.用法和普通的static方法一样. 代码示例 public interface InterfaceA { /** * 静 ...

  8. Asp.Net任务Task和线程Thread

    Task是.NET4.0加入的,跟线程池ThreadPool的功能类似,用Task开启新任务时,会从线程池中调用线程,而Thread每次实例化都会创建一个新的线程.任务(Task)是架构在线程之上的, ...

  9. [Memcached] telnet命令

    一:连接命令 在windows下的cmd或者Linux执行 telnet 127.0.0.1 11211 (如果此处报错"telnet不是内部或外部命令",一定是没有安装telne ...

  10. mybatis中设置打印sql语句application.yml

    在application.yml配置文件中,找到数据源设置,添加: mybatis: configuration: log-impl:org.apache.ibatis.logging.stdout. ...