HDU1242 Rescue
| Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
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
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
Sample Input
Sample Output
Source
由于引入了"杀死守卫"这一额外耗费时间的情况,所以第一次BFS到终点时的解不一定最优,应该不断更新经过节点的值,直到不能更新,跑完整个BFS队列,才能得到最优解
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;
const int INF=;
const int mxn=;
struct node{
int x,y;
int w;//用时
};
queue<node>q;
int mx[]={,,,-,},
my[]={,,,,-};
int ax,ay;
char mp[mxn][mxn];
int ans[mxn][mxn];
int n,m; void BFS(node s){
q.push(s);
int i;
node hd;
while(!q.empty()){
hd=q.front();
q.pop();
for(i=;i<=;i++){
int x=hd.x+mx[i];
int y=hd.y+my[i];
if(x> && x<=n && y> && y<=m && mp[x][y]!='#'){
node v;
v.x=x; v.y=y; v.w=hd.w+;
if(mp[x][y]=='x') v.w++;//杀死守卫要多花时间
if(v.w<ans[x][y]){
ans[x][y]=v.w;
q.push(v);
}
}
}
}
return;
}
int main(){
int i,j;
while(scanf("%d%d",&n,&m)!=EOF){
memset(mp,' ',sizeof(mp));
int sx,sy;
node ST;
for(i=;i<=n;i++){
scanf("%s",mp[i]+);
for(j=;j<=m;j++){
ans[i][j]=INF;
if(mp[i][j]=='a'){ax=i;ay=j;}
else if(mp[i][j]=='r'){sx=i;sy=j;}
else if(mp[i][j]!='.' && mp[i][j]!='x')mp[i][j]='#';
//从讨论区看到数据里可能有其他符号,也应算作#
}
}
ST.x=sx;ST.y=sy;ST.w=;
ans[sx][sy]=;
BFS(ST);
if(ans[ax][ay]!=INF)printf("%d\n",ans[ax][ay]);//有解
else printf("Poor ANGEL has to stay in the prison all his life.\n");//无解
}
return ;
}
HDU1242 Rescue的更多相关文章
- HDU1242 Rescue(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu1242 Rescue DFS(路径探索题)
这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.hdu.edu.cn/showproblem.php? ...
- 搜索专题: HDU1242 Rescue
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 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人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...
- hdu1010--Tempter of the Bone(迷宫)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Jav ...
- 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 ...
- Nova Suspend/Rescue 操作详解 - 每天5分钟玩转 OpenStack(35)
本节我们讨论 Suspend/Resume 和 Rescue/Unrescue 这两组操作. Suspend/Resume 有时需要长时间暂停 instance,可以通过 Suspend 操作将 in ...
- HDU1242 BFS+优先队列
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
随机推荐
- 如何修改myeclipse 内存,eclipse.ini中各个参数的作用。
修改MyEclipse/eclipse文件夹中配置文件eclipse.ini中的内存分配就哦了 =================================== 一般的ini文件设置主要包括以下 ...
- python 维吉尼亚
加密key='COMPUTER' plaintext='BLOCKCIPHERDESIGNPRINCIPLE' ascii='abcdefghijklmnopqrstuvwxyz'.upper() k ...
- smarty中三种变量的访问方式
在模板中smarty有三种变量,第一种,php分配的变量,第二种配置文件里的变量,第三种,PHP全局数组里的变量,配置文件里变量的访问方式可以是{#bgcolor#},"#"必须紧 ...
- 08SpringMvc_(1)继承AbstractCommandController的Action[能够以实体的形式,收集客户端参数].(2)日期转换器和编码过滤器
上一篇文章说过要介绍两个控制器.这篇文章就介绍第二个控制器AbstractCommandController(这个类已经快要被废弃了,有更好的代替者,但还是要好好学这个类).这个控制器的额作用是为了收 ...
- RDLC系列之七 条码打印
参考: C# 条码标签打印程序,RDLC报表动态显示多条码标签的方法 http://www.cnblogs.com/vice/p/4105898.html 我做的思路是:不使用数据库存储image的b ...
- win server 2008配置ftp无法登陆问题的解决办法
解决办法放在最前面,方便急需答案的同学: 创建了ftp使用的windows账户后,一定要给该账户添加ftp目录的权限,如下图所示,为新账户添加权限后(且设置了“ftp身份验证”),即可正常访问ftp: ...
- C语言 百炼成钢16
//题目46:海滩上有一堆桃子,五只猴子来分.第一只猴子把这堆桃子凭据分为五份,多了一个,这只 //猴子把多的一个扔入海中,拿走了一份.第二只猴子把剩下的桃子又平均分成五份,又多了 //一个,它同样把 ...
- 将Log4net的配置配置到的独立文件中
本文转载:http://blog.csdn.net/wanzhuan2010/article/details/7587780 另外一篇博客:http://grim1980.blog.sohu.com/ ...
- C#文件和文件文件夹按时间、名称排序-顺序与倒序
对于文件和文件夹有多种排序方式,常用的就是按创建或修改时间.按文件名排序.在 C# 中,按时间和文件名排序都十分简单,用数组提供的排序方法 Array.Sort() 一行代码就可以搞定,当然也可以用常 ...
- [word]2010中插入公式自动编号并且公式不自动缩小/变小
要实现在word2010中插入公式自动编号,就要用到自动图文集功能,具体操作如下: 1.先制定制表位位置:单击一个空白段落,然后双击标尺线的底部:这会激活"制表位"对话框,如图所示 ...