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. stylus or less ?

    为什么不说SASS? 因为它需要安装Ruby,而一般的前端开发人员是不会特地去安装Ruby.我似乎更喜欢nodejs! ok,那么我们怎么在stylus和less 之间做出一个好的选择呢? 首先我本人 ...

  2. python基础(八)-迭代器与生成器

    一.迭代器 li=[1,2,3] f=li.__iter__() print(f) print(f.__next__()) print(f.__next__()) print(f.__next__() ...

  3. win8,win10安装mysql

    以管理员身份进到命令窗口后,找到要安装的文件,执行msiexec /package mysql-installer-community-5.7.16.0.msi   回车即可

  4. checkbox:全选与反全选

    $(document).ready(function () { //全选checkbox $("#selectAll").click(function () { var check ...

  5. 解决getElementsByClassName兼容问题

    getElementsByClassName这个方法很常用,但是只有较新的浏览器才兼容,所以我们需要自己写个方法,解决这个问题,使它能够兼容各个浏览器. function getElementsByC ...

  6. centos 7 连接 xshell5

    首先 保证你的centos版本与你选择的linux版本相同. 1.首先查看本机IP和网关 2.在centos7命令行下输入nmtui 进入   Edit a commection 选择Edit 按照刚 ...

  7. ramfs/tmpfs: Linux RAM filesystems

    Previously in windows, I uses ramdisk, to accelerate chrome browsing. However, in linux, this is mad ...

  8. slf4j 之logback日志之sl4j架构【二】

    一.整体介绍 介绍: The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for v ...

  9. Arrar.prototype.map()的用法

    ---恢复内容开始--- map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组. array.map(callback[, thisArg]) 注:[]在语法中[]内的内 ...

  10. JAVA监听

    http://www.cnblogs.com/xdp-gacl/p/3961929.html 不要按汉字的字面意思理解这东西,其实它就是把某个操作绑到了某个按纽上,当你按那个按纽的时候,它所绑定的操作 ...