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
 
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
typedef struct nn
{
int x,y;
int time;
friend bool operator<(nn n1,nn n2)
{
return n2.time<n1.time;
}
}node;
int h,w,si,sj,di,dj,minT,flog;
int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
char map[205][205];
void BFS(void)
{
int tx,ty,e,k,i;
priority_queue<node> Q;
node q,p; q.time=0;q.x=sj;q.y=si;
Q.push(q);
while(!Q.empty())
{
q=Q.top();
Q.pop();
for(e=0;e<4;e++)
{
tx=q.x+dir[e][1];
ty=q.y+dir[e][0];
if(tx>=0&&tx<w&&ty>=0&&ty<h&&map[ty][tx]!='#')
{
p.time=q.time+1; p.x=tx; p.y=ty;
if(p.y==di&&p.x==dj)
{
minT=p.time;
flog=1;
return ;
}
if(map[ty][tx]=='x')
{
p.time+=1;//printf("#");
}
map[ty][tx]='#';
Q.push(p);
}
}
}
}
int main()
{
int i,j;
while(scanf("%d%d",&h,&w)>0)
{
for(i=0;i<h;i++)
{
getchar();
for(j=0;j<w;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='r')
{
si=i;sj=j;
}
if(map[i][j]=='a')
{
di=i;dj=j;
}
}
}
flog=0;
BFS();
if(flog==0)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",minT);
}
}

1242Rescue (优先队列BFS)的更多相关文章

  1. 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 ...

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

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

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

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

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

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

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

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

  6. HDU——1242Rescue(BFS+优先队列求点图最短路)

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

  7. 【UESTC 482】Charitable Exchange(优先队列+bfs)

    给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...

  8. cdoj 482 优先队列+bfs

    Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

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

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

随机推荐

  1. hdoj 2054(A==B)

    注意考虑以下数据: 123  123.0; 0.123  .123; 00.123  0.123; 代码: #include<iostream>#include<cstdio> ...

  2. demo_02 less

    html 中的代码<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...

  3. SVN遇到Can't convert string from 'UTF-8' to native encoding

    刚配好mysql,svn co代码的时候遇到问题 svn: Can't convert string from 'UTF-8' to native encoding: svn: platform/co ...

  4. [CSS]text-decoration

      定义和用法 text-decoration 属性规定添加到文本的修饰. 可能的值 值 描述 none 默认.定义标准的文本. underline 定义文本下的一条线. overline 定义文本上 ...

  5. finalspeed服务器端和客户端安装

    https://www.91yun.org/archives/2775 https://www.91yun.org/archives/615 1.首先安装服务器端:一键安装代码 wget -N --n ...

  6. iOS:实现表格填充和选择操作

    功能:创建一个列表,用数组填充表格,并支持选择列表行 // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright ...

  7. imacros实现Excel数据自动录入到网页中

    一.工具选择 最近接到一个项目,需要将excel数据逐条录入.保存到网页中.经过搜集资料,能实现功能的大概有以下几种方式,按键精灵.autoit.imacros.python+selenium. 按键 ...

  8. php pdo和mysqli对比选择

    1)总的比较   PDO MySQLi 数据库支持 12种不同的数据库支持 支持MySQL API OOP OOP + 过程 Connection Easy Easy 命名参数 支持 不支持 对象映射 ...

  9. 打log

    如果项目上过线的话,那你一定知道Log是多么重要. 为什么说Log重要呢?因为上线项目不允许你调试,你只能通过Log来分析问题.这时打一手好Log的重要性绝不亚于写一手好代码.项目出问题时,你要能拿出 ...

  10. jquery学会的

    1.$("#id")    $("xxxxx") (input, body) $(".class") 2. $("#id  xxx ...