Rescue


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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 <queue>
#include <string.h>
using namespace std;
struct node
{
int x,y,time;//x,y方格的位置。time当前全部的时间
friend bool operator<(node a,node b)//优先队列依照时间大小排序
{
return a.time>b.time;
}
};
priority_queue<node>s;
char map[205][205];//地图
int m,n,vis[205][205],escape,dir[4][2]={0,1,0,-1,1,0,-1,0};//vis标记,dir表示四个方向
bool judge(int x,int y)//推断当前位置时候能够走
{
if(x>=0&&y>=0&&x<n&&y<m&&!vis[x][y]&&map[x][y]!='#')
return true;
else
return false;
}
int tonum(int x,int y)//把对应的道路,警卫换算成时间
{
if(map[x][y]=='x')
return 2;
else
return 1;
}
void bfs(int x,int y)//广度优先搜索
{
node temp,temp1;
temp.time=0,temp.x=x,temp.y=y;
vis[x][y]=1;
s.push(temp);//把temp入队列
while(!s.empty())
{
temp=s.top(),s.pop();
temp1=temp;
if(map[temp.x][temp.y]=='a')//提前结束循环,由于用的优先队列。所以当前找到的肯定是最小的
{
escape=temp.time;
break;
}
for(int i=0;i<4;i++)
{
int xx=temp.x+dir[i][0];
int yy=temp.y+dir[i][1];
if(judge(xx,yy))
{
vis[xx][yy]=1;//当前位置已浏览 标记为1
temp.x=xx,temp.y=yy,temp.time=temp.time+tonum(xx,yy);
s.push(temp);//更新队列
}
temp=temp1;//由于才推断了一个方向。所以还须要temp1保持上次的位置
}
}
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
escape=0;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
scanf("%s",map[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='r')
{
bfs(i,j);
break;
}
if(escape)
printf("%d\n",escape);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
while(!s.empty())//一定要记得清队列。 。刚刚wa了一次
s.pop();
}
}

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

  1. HDU1242 Rescue(BFS+优先队列)

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

  2. hdu1242 Rescue bfs+优先队列

    直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...

  3. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  4. hdu1242 Rescue(BFS +优先队列 or BFS )

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:     Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...

  5. Rescue BFS+优先队列 杭电1242

    思路 : 优先队列 每次都取最小的时间,遇到了终点直接就输出 #include<iostream> #include<queue> #include<cstring> ...

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

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

  7. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  8. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  9. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

随机推荐

  1. 多线程(实现Runnable接口)

    /** * 创建一个子线程, 完成1-100之间自然数的输出,同样的主线程执行同样的操作 *创建多线程的第二种方式,通过实现的方式 * 继承和实现的方式对比 * 一,联系: *  public cla ...

  2. 【EX_BSGS】BZOJ1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 425  Solved: 238[Submit][Status ...

  3. Linux PC开发环境搭建建议

    搭建Linux PC开发环境 很早之前整理的在Linux(ubuntu)系统下搭建 PC开发环境的工具的推荐和简单说明,尽管现在有些已经不再使用,但还是要备份一下,作为以后的参考: package: ...

  4. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  5. struts 中继承ActionSupport类

    理论上Struts 2.0的Action无须实现任何接口或继承任何类型,但是,我们为了方便实现Action,大多数情况下都会继承 com.opensymphony.xwork2.ActionSuppo ...

  6. 让你的qstardict读单词

    作为编程行当的人员,英语是躲不掉的,很多资料英文更加有效,字典就显得尤为重要,我希望字典不但能查到中文意思,还能发生,那就跟我来吧: 一.安装字典程序: pacman -S qstartdic sox ...

  7. Day 23 异常处理

    异常处理 一.异常基本形式 # if # def test: # pass # class Foo # pass def test(): ''' 异常的基本类型和相关 :return: ''' try ...

  8. 转:C#制作ORM映射学习笔记三 ORM映射实现

    现在开始实现ORM的主体模块,首先需要在项目中新建一个类,命名为DbAccess,然后在项目的引用中添加两个dll,分别是MySql.Data.dll和System.Data.SQLite.dll,这 ...

  9. 在Alfred添加自定义站内搜索

    1.Google的站内搜索和渣度的对比,懒得吐槽了 2.在Alfred添加自定义站内搜索步骤 Add Custome Search 把搜索某个关键词的url复制到里面,把url里的关键词替换成{que ...

  10. 解决IE6的PNG透明

    http://www.jb51.net/article/35669.htm http://blog.csdn.net/mosliang/article/details/6760028