Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 521    Accepted Submission(s): 217
 
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

题意:要求天使的朋友在最短的时间内救出天使,a代表天使,r代表天使的朋友,.代表路,#代表墙,x代表小兵,

每走一步需要一个时间,遇到小兵杀了他要一个时间,移到小兵的位置有要一个时间;

输出最短的时间,

如果救不出来就输出“Poor ANGEL has to stay in the prison all his life.”

/*
题解:利用优先队列+bfs
这题一直错,原因竟然是。。。读入有多组数据,我只读了一组
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int f[][];
char mp[][];
int n,m,sx,sy;
struct node
{
int x,y;
node(int a,int b){x=a; y=b;}
};
int dr[][]={{,},{,},{-,},{,-} };
struct cmp
{
bool operator()(node a,node b)
{
return f[a.x][a.y]>f[b.x][b.y];
}
};
int bfs()
{
priority_queue<node,vector<node>,cmp> Q;
memset(f,,sizeof(f));
Q.push(node(sx,sy));
f[sx][sy]=;
while(!Q.empty())
{
node p=Q.top();
Q.pop();
for(int i=;i<;i++)
{
int xx=p.x+dr[i][];
int yy=p.y+dr[i][];
if (xx>=n || xx< || yy>=m || yy<) continue;
if (mp[xx][yy]=='#' || f[xx][yy]>) continue;
f[xx][yy]=f[p.x][p.y]+;
if (mp[xx][yy]=='x') f[xx][yy]++;
Q.push(node(xx,yy));
if(mp[xx][yy]=='r') return f[xx][yy]-;
}
}
return -;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<n;i++)
{
scanf("%s",&mp[i]);
for(int j=;j<m;j++)
if (mp[i][j]=='a') sx=i,sy=j;
}
int ans=bfs();
if (ans==-) printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",ans);
} return ;
}

ZOJ 649 Rescue(优先队列+bfs)的更多相关文章

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

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

  2. F - Rescue 优先队列bfs

    来源poj Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a ...

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

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

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

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

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

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

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

  7. hdu 1242:Rescue(BFS广搜 + 优先队列)

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

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

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

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

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

随机推荐

  1. reids配置参数详解

    转自:http://www.jb51.net/article/60627.htm reids配置参数详解 #daemonize no  默认情况下, redis 不是在后台运行的,如果需要在后台运行, ...

  2. php 便利数组方法

    数组在PHP中是一个非常强大的武器,用起来方便.容易,由于使用起来异常灵活,用它就可以实现数据结构中的链表.栈.队列.堆以及所谓的字典.集合等,也可以转换成XML格式. 1.使用for for语句遍历 ...

  3. Eclipse Bug: Unhandled event loop exception No more handles

    我的解决方法如下: I had the same problem, turned out that TeamViewer was causing this. In your TeamViewer go ...

  4. in_array 判断的一些见解

    我个人见解in_array的判断是== 并不是===  证明如下: $arr=(array_merge(range(1, 9),range('a', 'z'),range('A', 'Z')));$m ...

  5. APP性能测试工具

    各种自动化工具作用: 自动化:appium 针对接口做压测:jmeter 执行一段脚本,随机乱点:monkey 如果有用loadrunner12的话 也可以用mobilerecoder录制脚本(lr1 ...

  6. “System.BadImageFormatException”类型的未经处理的异常在 PurchaseDevices.Access.dll 中发生 其他信息: 未能加载文件或程序集“System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139”或它的某一个依赖项。试图加载格式不正确

    引用sqlite的程序集时,有时会报如下异常:  "System.BadImageFormatException"类型的未经处理的异常在 PurchaseDevices.Acces ...

  7. zoj 3204 Connect them

    最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...

  8. linux搭建apache服务并修改默认路径

    该篇文章主要讲解如何在linux服务器上搭建apache服务器,并修改指定的apache路径到自定义路径下 一:检查服务器上是否已安装apache,停止并卸载系统自带apache服务 命令为:rpm ...

  9. 第一百一十二节,JavaScript浏览器检测

    JavaScript浏览器检测 学习要点: 1.navigator对象 2.客户端检测 由于每个浏览器都具有自己独到的扩展,所以在开发阶段来判断浏览器是一个非常重要的步骤.虽然浏览器开发商在公共接口方 ...

  10. radio的选中设置以及取值。

    前台:<input type=" id="tg" name="state"/> <a style="cursor:poin ...