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 ...
随机推荐
- mysqli预处理和事务处理
1 应用环境 mysqli预处理功能(大量数据处理时使用) 2 步骤 a)mysqli连接数据库 $mysqli = new mysqli('localhost','root','root','chu ...
- cocoaPod相关问题
cocoap简介: 1. 简介 CocoaPods是一个负责管理iOS项目中第三方开源代码的工具,其源码在Github上开源.使用CocoaPods可以节省设置和更新第三方开源库的时间并提高工作效率. ...
- Linux 进程与线程四(加锁--解锁)
线程共享进程的内存空间,打开的文件描述符,全局变量. 当有多个线程同事访问一块内存空间或者一个变量.一个文件描述符,如果不加控制,那么可能会出现意想不到的结果. 原子操作 对于我们的高级语言(C语言, ...
- 多个相同jar存在时的引用顺序
起因:今天一个aar包在测试环境中正常运行,使用soapui测试正常返回,在本地环境中运行则老是报数据库连接异常,经检查,是因为在运行时环境中缺少ojdbc相关的jar包引起的. 重新打了一个aar包 ...
- (OAF)jdeveloper集成log4j并将日志输出到指定文件并写入数据库
参考: How to configure Log4j in JDev 11g Ever wanted to use log4j in your adf project ? Well though Or ...
- Java开发环境的搭建
确定自己的操作系统版本并下载安装JDK 1.下载JDK windows系统: 右键我的电脑->属性;如下图: 2.下载JDK 下载地址:http://www.oracle.com/index.h ...
- 用python简单处理图片(2):图像通道\几何变换\裁剪
一.图像通道 1.彩色图像转灰度图 from PIL import Image import matplotlib.pyplot as plt img=Image.open('d:/ex.jpg') ...
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...
- 20135208 第一次JAVA实验报告
课程:Java程序设计 班级: 1352 姓名:贺邦 学号:20135208 成绩: 指导教师:娄嘉鹏 实验日期:2 ...
- HDU5802-windows 10-dfs+贪心
音量减的时候,分两种,一种是减到大于目标M,另一种是减到小于M,停顿的时候可以减少最后往上加的次数,小于0的时候变成0 然后比一下这两种的最小值. /*------------------------ ...