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

这题是说Angle被人抓住了,她的朋友去救她。。而x是阻碍他的敌人,可是Angle的朋友足够强,能消灭全部敌人,可是在消灭敌人的同一时候,

他要多花费1秒的时间,题目要你求出他解救Angle所需的最短时间。。

这道题用BFS,但要注意的是最短路径也可能并非最优解。。

所以我们用定义一个二维数组来记录每一个位置时的最短时间,在BFS搜索的过程中,到某个位置时,搜索的结果大于最短时间时,不入队。

这样就攻克了本题的问题了。

此外,不要一推断到达目的位置时就推出BFS返回结果。。我就是由于在这里WA了一发。。由于这可能不是最优解。。仅仅有等到队列为空或

BFS搜索结束时,才干得出最优解或者是“无法到达目的位置”。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath> using namespace std; const int maxn = 205;
const int INF = 100000;//用于初始每一个位置的时间,尽可能开大点
char map[maxn][maxn];//地图
int mintime[maxn][maxn];//记录每一个位置的最小时间
int n, m;//地图的大小
int ax, ay;//Angel的位置
int fx, fy;//Angel的friend的位置
int ans;//结果
//地图的走法
int dx[] = { -1, 0, 0, 1 };
int dy[] = { 0, -1, 1, 0 }; struct point
{
int x;
int y;//方格的位置
int time;//记录当前走到这所花费的时间
};
queue<point>Q;//队列 int BFS()//BFS函数
{
while( !Q.empty() )
{
point temp = Q.front();//记录队首元素
Q.pop();
// if( temp.x==ax && temp.y==ay )
//return mintime[ax][ay]; 開始我在这输出,使得WA。。由于这可能不是最优解
for(int i=0; i<4; i++)
{
int xx = temp.x + dx[i];
int yy = temp.y + dy[i];
if( xx>=0 && xx<n && yy>=0 && yy<m && map[xx][yy]!='#' )//排除边界以及墙壁
{
point next;//下一个位置
next.x = xx;
next.y = yy;
next.time = temp.time + 1;
if( map[xx][yy] == 'x' ) next.time += 1;//若遇到敌人,消灭其需加1
if( next.time < mintime[xx][yy] )//若这样的走法所用的时间比原来mintime小则入队
{
mintime[xx][yy] = next.time;
Q.push(next);
}
}
}
}
return mintime[ax][ay];//返回结果
} void output()//输出
{
if( ans < INF )
printf("%d\n", ans);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
} void init()//输入以及初始化
{
while(scanf("%d%d", &n, &m)==2)
{
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++)
{
mintime[i][j] = INF;//初始每一个位置的时间
if( map[i][j] == 'r' ) //记录friend的位置
{
fx = i;
fy=j;
}
if( map[i][j] == 'a' )//记录Angel的位置
{
ax = i;
ay = j;
}
}
point stay;
stay.x = fx;
stay.y = fy;
stay.time = 0;
mintime[fx][fy] = 0;
Q.push( stay );//BFS的開始
ans = BFS();
output();
}
} int main()
{
init(); return 0;
}

ZOJ 1649:Rescue(BFS)的更多相关文章

  1. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  2. 程序员的算法课(18)-常用的图算法:广度优先(BFS)

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...

  3. Rescue(bfs)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  4. ZOJ 1005:Jugs(思维)

    Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Har ...

  5. ZOJ 1091 Knight Moves(BFS)

    Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are t ...

  6. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

  7. SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...

  8. 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)

    参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...

  9. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

随机推荐

  1. Jenkins(转)

    1  修改jenkins的根目录,默认地在C:\Documents and Settings\AAA\.jenkins . .jenkins ├─jobs│  └─JavaHelloWorld│    ...

  2. Java 对象的生命周期

    Java对象的生命周期 在Java中,对象的生命周期包含下面几个阶段: 1.      创建阶段(Created) 2.      应用阶段(In Use) 3.      不可见阶段(Invisib ...

  3. hdu3844 Mining Your Own Business,无向双连接组件

    点击打开链接 无向图的双连通分量 #include<cstdio> #include<stack> #include<vector> #include<map ...

  4. 接近带给你AngularJS - 经验说明示例

    接近带给你AngularJS列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自己定义指令 ------------ ...

  5. ECharts SSH+JQueryAjax+Json+JSP在数据库中的数据来填充ECharts在

    1导入包.设定SSH框架. 进口JQuery的JS包.<script src="JS/jquery-1.7.1.js"></script> 导入EChart ...

  6. 读书时间《JavaScript高级程序设计》二:面向对象

    接着上次的进度,开始看第6章. 第6章 面向对象的程序设计 理解对象 创建自定义对象最简单的方式就是创建一个 Object 的实例,然后为它添加属性和方法. var person = new Obje ...

  7. java main方法背后的故事?(转)

    jvm java 看似一种语言,实则一个巨大的体系的王国,开发这么多年了,还是没有搞懂,我以为我懂了,可是过了一段时间又忘了,所以说还是没懂 1.main方法说起 编译完我们的java文件后,需要有个 ...

  8. 最艰难的采访IT公司ThoughtWorks代码挑战——FizzBuzzWhizz游戏

    最近的互联网招聘平台拉勾网在五月推出了"最艰难的采访IT公司"码挑战活动,评选出了5个最难面试的IT公司,即:ThoughtWorks.Google.Unisys.Rackspac ...

  9. 默认情况下安装的应用程序C盘后提示权限不足,当你开始介意。。。

    最近,不少用户抱怨的经销商.正在使用win 7我们的计算机系统上安装软件后,提示权限不够开放系统,无法启动软件. 在xp该系统是没有问题的.原因是,我们会选择在默认安装路径系统C-disk.和win ...

  10. JTextArea Demo

    在往JTextArea中填充数据时,JTextArea上的滚动条也可以拖动.解决办法:主线程放在EDT中,fill JTextArea的操作放在另外一个线程中,这样fill操作与GUI上的操作就分离了 ...