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 ...
随机推荐
- mysql怎么查询前10条数据?
mysql 没有top的用法.取而代之的是limit语法为:limit m,n省略n就可以得到你要的效果了. select * from table1 order by column desc li ...
- 富有表现力的javascript
1.javascript的灵活性,你可以把它写的很简单,也可以写的很复杂,简直就是随心所欲: 2.javascript是弱类型语言,定义变量的时候不用声明变量类型,不声明类型,并不是说,javascr ...
- 一篇文章告诉你为何GitHub估值能达20亿美元
软件开发平台GitHub今日宣布,已获得硅谷多家知名风投2.5亿美元融资,这也让其融资总额达到了3.5亿美元,此轮融资对GitHub的估值约为20亿美元. GitHub有何特别之处? GitHub创立 ...
- iOS页面传值方式
普遍传值方式如下: 1.委托delegate方式: 2.通知notification方式: 3.block方式: 4.UserDefault或者文件方式: 5.单例模式方式: 6.通过设置属性,实现页 ...
- Discuz X3核心文件解析
<?php /** * [Discuz!] (C)2001-2099 Comsenz Inc. * This is NOT a freeware, use is subjec ...
- 微信公众平台开发之微信access_token如何有效长期保存
随着微信使用越来越广泛,微信公众平台开放了许多接口以提供更多个性化的服务,包括自定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等,开发者在调用这些接口时,都需要传入一个相同的参数ac ...
- Asp.net设计模式笔记之二:应用程序分离与关注点分离
本次笔记主要涉及的内容如下: 1.将智能UI(SmartUI)反模式重构成分层方式的示例代码 2.分层设计与传统的Asp.net WebForm模型(代码后植)相比具有的优势 3.逻辑分层概念以及分离 ...
- 免费Flash图表工具FusionChart
图表显示是很多开发工作所必不可少的一项功能,今天我介绍一个前段时间发现的免费的Flash图表开发工具,可以通过Adobe Flash实现数据的图表化,动态化以及相互交互. FusionChart是一个 ...
- [CareerCup] 7.6 The Line Passes the Most Number of Points 经过最多点的直线
7.6 Given a two-dimensional graph with points on it, find a line which passes the most number of poi ...
- 20145222黄亚奇《Java程序设计》实验二实验报告
20145222<Java程序设计>第2次实验报告 实验步骤与内容 一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L. ...