G - Rescue 【地图型BFS+优先队列(有障碍物)】
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.)
InputFirst 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.
OutputFor 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."*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3fffffff
using namespace std; const int maxn = ; int n,m,xxx,yyy;
char a[maxn][maxn];
int vis[maxn][maxn];
int dir[][]={ {,},{,},{-,},{,-} }; struct Node
{
int xx,yy,time; //横纵坐标+时间
friend bool operator < (Node a,Node b)
{
return a.time > b.time;
}
}; int check(int x,int y)
{
if(x<||x>=n||y<||y>=m||a[x][y]=='#'||vis[x][y])
return ;
return ;
}
/*int check(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || a[x][y] == '#')
return 1;
return 0;
}*/ int bfs(int x,int y)
{
memset(vis,,sizeof(vis));//清空标记数组放到bfs里面!!!!!
priority_queue<Node> q;
while(!q.empty()) q.pop();
q.push(Node{x,y,});
vis[x][y]=; while(!q.empty())
{
Node u=q.top();
q.pop();
Node tmp;
if(a[u.xx][u.yy]=='r')
return u.time;
for(int i=;i<;i++)
{
tmp.xx=u.xx+dir[i][];
tmp.yy=u.yy+dir[i][]; if( check(tmp.xx,tmp.yy) )
{
vis[tmp.xx][tmp.yy]=;
if(a[tmp.xx][tmp.yy]=='x')
tmp.time=u.time+;
else
tmp.time=u.time+;
q.push(tmp);
}
}
}
return -;
} int main()
{
memset(vis,,sizeof(vis));
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<n;i++)
{
scanf("%s",&a[i]);
} for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j]=='a')
{
xxx=i;
yyy=j;
break;
}
}
} int ans=bfs(xxx,yyy);
if(ans==-)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
} }
G - Rescue 【地图型BFS+优先队列(有障碍物)】的更多相关文章
- C - 你经历过绝望吗?两次! 【地图型BFS+优先队列(障碍物)】
4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的“猪坚强”.不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川“猪坚 ...
- Rescue HDU1242 (BFS+优先队列) 标签: 搜索 2016-05-04 22:21 69人阅读 评论(0)
Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...
- D - Interesting Calculator 【数值型BFS+优先队列】
There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...
- F - Oil Deposits 【地图型BFS+联通性】
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- B - ACM小组的古怪象棋 【地图型BFS+特殊方向】
ACM小组的Samsara和Staginner对中国象棋特别感兴趣,尤其对马(可能是因为这个棋子的走法比较多吧)的使用进行深入研究.今天他们又在 构思一个古怪的棋局:假如Samsara只有一个马了,而 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- 2018年长沙理工大学第十三届程序设计竞赛 G 逃离迷宫 【BFS】
链接:https://www.nowcoder.com/acm/contest/96/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- hdu 5040 Instrusive【BFS+优先队列】
11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...
随机推荐
- 【bzoj2453】维护队列/【bzoj2120】数颜色 分块+二分
题目描述 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会依据个人喜好, ...
- WebSocket简单介绍(1)
HTML5作为下一代WEB标准,拥有许多引人注目的新特性,如Canvas.本地存储.多媒体编程接口.WebSocket等等.今天我们就来看看具有“Web TCP”之称的WebSocket. WebSo ...
- P1559 运动员最佳匹配问题
题目描述 羽毛球队有男女运动员各n人.给定2 个n×n矩阵P和Q.P[i][j]是男运动员i和女运动员j配对组成混合双打的男运动员竞赛优势:Q[i][j]是女运动员i和男运动员j配合的女运动员竞赛优势 ...
- [SCOI2010]序列操作 线段树
---题面--- 题解: 在考场上打的这道题,出人意料的很快就打完了?! 直接用线段树,维护几个东西: 1,lazy标记 : 表示区间赋值 2,mark标记:表示区间翻转 3,l1:前缀最长连续的1的 ...
- GDI绘图中的映射模式CDC::SetMapMode()
原文链接:http://blog.csdn.net/charlessimonyi/article/details/8264572 在GDI绘图前,一般要设置映射模式.映射模式是什么呢?它是逻辑长度单位 ...
- POJ3261 Milk Patterns 【后缀数组】
牛奶模式 时间限制: 5000MS 内存限制: 65536K 提交总数: 16796 接受: 7422 案件时间限制: 2000MS 描述 农夫约翰已经注意到,他的牛奶的质量每天都在变化.经进 ...
- 【BZOJ 2957】楼房重建&&Codechef COT5 Count on a Treap&&【NOIP模拟赛】Weed 线段树的分治维护
线段树是一种作用于静态区间上的数据结构,可以高效查询连续区间和单点,类似于一种静态的分治.他最迷人的地方在于“lazy标记”,对于lazy标记一般随我们从父区间进入子区间而下传,最终给到叶子节点,但还 ...
- Codeforces Round #520 (Div. 2) A. A Prank
A. A Prank time limit per test 1 second memory limit per test 256 megabytes 题目链接:https://codefo ...
- Python-Jenkins API使用
一.概述 最近在工作中需要用到在后台代码中触发Jenkins任务的构建,于是想到Jenkins是否有一些已经封装好的API类库提供,用于处理跟Jenkins相关的操作.下面就简单介绍下我的发现. 二. ...
- js 读写文件
读写文件: var f = fso.CreateTextFile("c:\\pexam\\"+name+".txt", true); f.write(arr); ...