HDU——1242Rescue(BFS+优先队列求点图最短路)
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25203 Accepted Submission(s): 8936
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.
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
自从懂了点BFS,这些就都是水过了。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
const int N=205;
char pos[N][N];
int vis[N][N];
int n,m;
struct info
{
int x;
int y;
int time;
bool operator<(const info &b)const
{
return time>b.time;
}
};
inline info operator+(const info &a,const info &b)
{
info c;
c.x=a.x+b.x;
c.y=a.y+b.y;
return c;
}
info direct[4]={{1,0,0},{0,1,0},{-1,0,0},{0,-1,0}};
info S;
priority_queue<info>Q;
void init()
{
MM(pos);
MM(vis);
while (!Q.empty())
Q.pop();
}
bool check(const info &a)
{
return (a.x>=0&&a.x<n&&a.y>=0&&a.y<m&&!vis[a.x][a.y]&&pos[a.x][a.y]!='#');
}
int main(void)
{
int i,j;
while (~scanf("%d%d",&n,&m))
{
init();
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
cin>>pos[i][j];
if(pos[i][j]=='a')
{
S.x=i;
S.y=j;
}
}
}
int r=-1;
S.time=0;
vis[S.x][S.y]=1;
Q.push(S);
while (!Q.empty())
{
info now=Q.top();
Q.pop();
if(pos[now.x][now.y]=='r')
{
r=now.time;
break;
}
for (i=0; i<4; i++)
{
info v=now+direct[i];
if(check(v))
{
v.time=now.time+(pos[v.x][v.y]=='x'?2:1);
vis[v.x][v.y]=1;
Q.push(v);
}
}
}
r==-1?puts("Poor ANGEL has to stay in the prison all his life."):printf("%d\n",r);
}
return 0;
}
HDU——1242Rescue(BFS+优先队列求点图最短路)的更多相关文章
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压
C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...
- hdu 1242(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
随机推荐
- arcgis jsapi接口入门系列(4):用代码在地图画点线面
PS:用代码画点这样写是为了跟后面的用鼠标画点线面区分出来 画点 drawPointGraphic: function () { //点有多种样式:一般的点,显示文字,显示图片 //一般的点 let ...
- [windows]命令行关机或重启电脑
1.关机:菜单--〉运行--〉输入:cmd--〉输入:shutdown -s -t 0 2.重启:菜单--〉运行--〉输入:cmd--〉输入:shutdown -r -t 0 (注:“-r”代表重启, ...
- win7下如何解决协议适配器错误问题
数据库为oracle 11g,在cmd中使用sqlplus命令出现了“协议适配器错误”. 原因分析:oracle相关服务没有启动. 解决办法如下: step1:进入服务页面. 方法一:cmd → se ...
- Servlet和JSP之JSTL学习
JSTL JSTL就是JSP标准标签库(JavaServer Pages Standard Tag Library, JSTL)是一个定制标签库的集合,用来解决像遍历Map或集合.条件测试.XML处理 ...
- Django添加tinyMCE编辑器
tinymce的使用方法很简单,只需要在html页面中包含如下: <!-- Place inside the <head> of your HTML --> <scrip ...
- Thread源码分析-java8
1.Thread特性分析 守护线程Daemon 定性:支持性线程,主要用于程序中后台调度以及支持性工作. 当JVM中不存在Daemon线程时,JVM将会退出. 将一个线程设定为Daemon的方法: 调 ...
- javascript基本数据类型问题汇总
isNaN()检测是否是NaN: 比较浮点相等,用绝对值,是否小于某一个阈值 Math.abs(1/3 - (1-2/3))<0.0000001: 字符串多行显示\n,ES6中使用反引号``: ...
- EMVS: Event-based Multi-View Stereo 阅读笔记
0. 摘要 EMVS目的:从已知轨迹的event相机,估计半稠密的3D结构 传统的MVS算法目的:从已知视点的图片集,去估计场景的稠密3D结构. EMVS2个固有属性: (1) 当传感器发生相对运 ...
- C06 变量和存储类型
目录 全局变量 局部变量 存储类型 全局变量和局部变量 变量的作用域 作用域:某些事物起作用或有效的区域. 变量的使用范围称为变量的作用域. 变量的作用域决定了变量的可操作性和有效性. C语言变量的作 ...
- iOS开发各种证书问题
引言 写在前面 一.App ID(bundle identifier) 二.设备(Device) 三.开发证书(Certificates) 四.供应配置文件(Provisioning ...