HDU1242 Rescue(BFS+优先队列)
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20938 Accepted Submission(s): 7486
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.)
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.
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."
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 200+10
int n,m,flag;
struct Node
{
int x,y,cost;
bool operator < (const Node &a)const//定义时间花费少的优先级高。
{
return cost>a.cost;
}
};
Node st,et,f,k;
int dx[]={,,-,};
int dy[]={,-,,};
int vis[maxn][maxn];
char map[maxn][maxn];
void bfs()
{
priority_queue<Node>q;
q.push(st);
while(!q.empty())
{
f=q.top();
for(int i=;i<;i++)
{
k.x=f.x+dx[i];
k.y=f.y+dy[i];
if(!vis[k.x][k.y]&&k.x>=&&k.x<n&&k.y>=&&k.y<m)
{
vis[k.x][k.y]=;
k.cost=f.cost+;
if(map[k.x][k.y]=='x')
k.cost++;
if(k.x==et.x&&k.y==et.y)
{
printf("%d\n",k.cost);
return;
}
q.push(k);
}
}
q.pop();
}
printf("Poor ANGEL has to stay in the prison all his life.\n");
} int main()
{
while(~scanf("%d%d",&n,&m))
{
if(m==)
break;
memset(vis,,sizeof(vis));
getchar();
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='#')
{
vis[i][j]=;
}
if(map[i][j]=='r')
{
st.x=i;
st.y=j;
st.cost=;
}
if(map[i][j]=='a')
{
et.x=i;
et.y=j;
} }
getchar();
}
bfs();
} return ;
}
HDU1242 Rescue(BFS+优先队列)的更多相关文章
- hdu1242 Rescue bfs+优先队列
直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...
- hdu1242 Rescue(BFS +优先队列 or BFS )
http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意: Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...
- 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 ...
- poj1649 Rescue(BFS+优先队列)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- Rescue BFS+优先队列 杭电1242
思路 : 优先队列 每次都取最小的时间,遇到了终点直接就输出 #include<iostream> #include<queue> #include<cstring> ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
随机推荐
- Python开发过程中17个坑
一.不要使用可变对象作为函数默认值 复制代码代码如下: In [1]: def append_to_list(value, def_list=[]): ...: def_list. ...
- mysql-error --(ERROR 1135 (HY000): Can't create a new thread (errno 11); if you are not out)
报错信息: ERROR 1135 (HY000): Can't create a new thread (errno 11); if you are not out 解决办法: # 查看限制情况 [r ...
- php字符串标点等字符截取不乱吗 封装方法
方法一: /** +---------------------------------------------------------- * 功能:字符串截取指定长度 * leo.li hen ...
- thinkphp框架的路径问题 - 总结
thinkphp框架的路径问题 - 总结 (2011-06-21 11:01:28) 转载▼ 标签: thinkphp 框架 路径 杂谈 分类: Php TP中有不少路径的便捷使用方法,比如模板中使用 ...
- [Spring入门学习笔记][Spring的AOP原理]
AOP是什么? 面向切面编程 软件工程有一个基本原则叫做“关注点分离”(Concern Separation),通俗的理解就是不同的问题交给不同的部分去解决,每部分专注于解决自己的问题.这年头互联网也 ...
- 禁止 favicon.ico 请求
favicon.ico 图标用于收藏夹图标和浏览器标签上的显示,如果不设置,浏览器会请求网站根目录的这个图标,如果网站根目录也没有这图标会产生 404.出于优化的考虑,要么就有这个图标,要么就禁止产生 ...
- EffectiveC#16--垃圾最小化
1.申请和释放一个基于堆内存的对象要花上更多的处理器时间. 所以当一个引用类型的局部变量在常规的函数调用中使用的非常频繁时应该把它提升为对象的成员(方法一) 2.当你把一个实现了IDisposable ...
- 初学jquery遇见的两个小问题!
<body> <div id="divtest">div的内容</div> <div id="default&quo ...
- Html5移动端页面布局通用模板暨移动端问题总结
最近的移动端项目终于告一段落了,其中遇到了不少问题,在此和大家总结分享. 首先,说一下结构.一般的手机页面大致可以分为五块:head, content, foot,solidbar,dialog. 针 ...
- 批量数据上传的sql.xml
<!-- User.xml --><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE ...