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. 导出文本、表格、图像到PDF格式文件中(学习整理)

    1.测试例子: 需要导入的外部jar包: 相关API http://www.coderanch.com/how-to/javadoc/itext-2.1.7/com/lowagie/text/pack ...

  2. redis数据类型(字符串)

    字符串 这是最简单Redis类型.如果你只用这种类型,Redis就像一个可以持久化的memcached服务器 127.0.0.1:6379> set mykey somevalue OK 127 ...

  3. Setup FTP Server On CentOS, RHEL, Scientific Linux 6.5/6.4/6.3

    setsebool allow_ftpd_full_access onsetsebool -P ftp_home_dir on vsftpd (Very Secure File Transport P ...

  4. mysql备份sql,脚本

    MySQL 安装位置:/usr/local/mysq 论坛数据库名称为:bbs MySQL root 密码:123456 数据库备份目的地:/var/db_backup/ #! /bin/bash / ...

  5. Hibernate 使用说明

    Eclipse中hibernate连接mySQL数据库练习(采用的是hibernate中XML配置方式连接数据库,以后在更新其他方式的连接) Hibernate就是Java后台数据库持久层的框架,也是 ...

  6. 【转】使用spring @Scheduled注解执行定时任务

    http://blog.csdn.net/sd4000784/article/details/7745947 以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在s ...

  7. Scroll view 备忘

    Stroyboard中使用ScrollView 当我们使用Storyboard开发项目时,如果要往控制器上拖入一个ScrollView并且添加约束设置滚动区域,是有特殊的规定的: 拖入一个scroll ...

  8. UFLDL教程(四)之Softmax回归

    关于Andrew Ng的machine learning课程中,有一章专门讲解逻辑回归(Logistic回归),具体课程笔记见另一篇文章. 下面,对Logistic回归做一个简单的小结: 给定一个待分 ...

  9. Hdu5517 Triple

    Description Given the finite multi-set \(A\) of \(n\) pairs of integers, an another finite multi-set ...

  10. [BZOJ 3888] [Usaco2015 Jan] Stampede 【线段树】

    题目链接:BZOJ - 3888 题目分析 首先,计算出每个线段在 x 坐标 0 处出现的时间开始点和结束点,就转成了时间轴上的线段. 然后就是看每条线段是否被 y 比它小的线段完全覆盖了.注意求出的 ...