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 ...
随机推荐
- Oracle导入导出dmp文件
目 录 目 录...3 1 说明...3 2 导出dmp文件...3 3 导入dmp文件...5 3.1 环境准备...5 ...
- a标签中有点击事件
我们常用的在a标签中有点击事件:1. a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题 ...
- 分享一个刷网页PV的python小脚本
下面分享一个小脚本,用来刷网页PV. [root@huanqiu ~]# cat www.py #!/usr/bin/python# coding: UTF-8import webbrowser as ...
- poj 1163 The Triangle
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43809 Accepted: 26430 De ...
- usb驱动开发5之总线设备与接口
Linux设备模型中的总线落实在USB子系统里就是usb_bus_type,它在usb_init的函数bus_register(&usb_bus_type)里注册.usb_bus_type定义 ...
- 房产企业如何借助K2 BPM脱颖而出?
点击这里,查看完整版房地产行业的流程管理解决方案.
- Silverlight中使用MVVM:DataGrid中触发Button的Click事件
方法1.使用RelativeSource向上查找DataContext中的命令,但是需要注意的是命令绑定需要写全 类似: DataContext.ReLoadCommand<Button Gri ...
- C语言 读取文件中特定数据
//读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...
- Linux经常用到的命令
1. Linux下用vim打开配置文件乱码,在终端输入:“LANG=”即可. 2. 查看端口是否被占用: 3. netstat -anp | grep port netstat -ltn 4. lso ...
- http状态码代表含义
状态代码 状态信息 含义 100 Continue 初始的请求已经接受,客户应当继续发送请求的其余部分.(HTTP 1.1新) 101 Switching Protocols 服务器将遵从客户的请求转 ...