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. ef code first 您没有所需权限

    在进行数据库更新操作时,update-database -force,反复提示没有改表或者没有所需权限,昨晚折腾了4个小时没有搞定,太晚了,今天Google了一下,这方面的错误,提到的都很少,在一篇文 ...

  2. FORM提交请求后自动打开输出EDITOR_PKG.REPORT

    DECLARE p_mode_request_id number := 1; p_ERRBUF VARCHAR2(250); p_RETCODE NUMBER; lv_msg varchar2(50) ...

  3. Shell学习笔记 ——第二天

    1.显示日期 date  |   cal   cal 2010  cal 2 2010 2.改变文件拥有者 chown 3.改变文件权限 chmod 4.显示当前目录 pwd 5.查看文件尾部内容,并 ...

  4. JAVA类与对象(课堂总结)

    一:"=="的不同含义 当"=="施加于原始数据类型变量时,是比较变量所保存的数据是否相等当"=="施加于引用类型变量时,是比较这两个变量是 ...

  5. [转]cmd-bat批处理命令延时方法

    批处理延时启动的几个方法 方法一:ping 缺点:时间精度为1秒,不够精确 @echo off @ping 127.0.0.1 -n 6 >nul start gdh.txt 方法二:vbs s ...

  6. HTML 标记

  7. 博客word测试

    博客word测试 博客word测试 from __future__ import division, print_functionDOCLINES = (__doc__ or '').split(&q ...

  8. 【Sort】QuickSort

    快速排序,平均运行时间O(N log N),最坏运行时间O(N^2). 我觉得先看Python版的快排算法(http://www.cnblogs.com/fcyworld/p/6160558.html ...

  9. ggplot2 theme相关设置—文本调整

    在geom设置和scale设置之后,要想把图画的漂亮,theme设置是比不可少的 在theme 设置中element_text()是一项很重要的内容 element_text(family = NUL ...

  10. POJ 3070 矩阵快速幂解决fib问题

    矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...