HDOJ(1242)BFS+优先队列
Rescue
http://acm.hdu.edu.cn/showproblem.php?pid=1242
题意:"#"是墙,"."是路,"a"是要被救的人,"r"是救援者,"x"是guard。每移动一步,需要一个单位时间。杀死guard也需要一个单位时间。求r到a的最短时间。
第一次听说优先队列,不得不承认我还是太弱了!!!
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
char map[][];
bool flag[][];
int dx[]={,,-,};
int dy[]={,,,-};
int n,m;
struct node
{
int x;
int y;
int time;
friend bool operator<(node a,node b) //优先队列
{
return a.time>b.time; //时间小的先出队
}
};
int BFS(int x,int y)
{
priority_queue<node> q;
node now,next;
int i;
now.x=x;
now.y=y;
now.time=;
q.push(now);
flag[now.y][now.x]=true; while(!q.empty())
{
now=q.top();
for(i=;i<;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];
if(next.x>=&&next.x<=m&&next.y>=&&next.y<=n&&map[next.y][next.x]!='#'&&flag[next.y][next.x]==false)
{
flag[next.y][next.x]=true;
next.time=now.time+;
if(map[next.y][next.x]=='x')
next.time++; q.push(next); //next更新后在入队
if(map[next.y][next.x]=='a')
return next.time;
}
}
q.pop();
}
return -;
}
int main()
{
int i,j,xe,ye;
while(scanf("%d%d",&n,&m)!=EOF)
{
vector<node> r;
r.clear();
for(i=;i<=n;i++)
{
getchar();
for(j=;j<=m;j++)
{
scanf("%c",&map[i][j]);
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(map[i][j]=='r')
{
node temp;
temp.y=i;
temp.x=j;
temp.time=;
r.push_back(temp);
}
}
} int min=;
for(i=;i<r.size();i++)
{
memset(flag,false,sizeof(flag));
int tem=BFS(r[i].x,r[i].y);
if(tem<min)
min=tem;
}
if(min<||r.size()==) //要判断是否有r,之前没判断,WA了几次
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",min);
}
return ;
}
这个是转得网上的 :
在优先队列中,优先级高的元素先出队列。
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
优先队列的第一种用法,也是最常用的用法:
priority_queue<int> qi;
通过<操作符可知在整数中元素大的优先级高。
第二种方法:
在示例1中,如果我们要把元素从小到大输出怎么办呢?
这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。
priority_queue<int, vector<int>, greater<int> >qi2;
第三种方法:
自定义优先级。
struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};
在该结构中,value为值,priority为优先级。
通过自定义operator<操作符来比较元素中的优先级。
但如果结构定义如下:
struct node
{
friend bool operator> (node n1, node n2)
{
return n1.priority > n2.priority;
}
int priority;
int value;
};则会编译不过(G++编译器)
因为标准库默认使用元素类型的“<”操作符来确定它们之间的优先级关系。
而且自定义类型的“<”操作符与“>”操作符并无直接联系,故会编译不过。
补充的一点是:
我们可以自己写一个比较类,还设定优先级:
struct cmp
{
bool operator() (const int &a, const int &b)
{
return dist[a] > dist[b];
}
};
priority_queue<int,vector<int>,cmp> q;(按照元素的dist大小从小到大排序).
HDOJ(1242)BFS+优先队列的更多相关文章
- hdu 1242(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- BFS+优先队列+状态压缩DP+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- D. Lunar New Year and a Wander bfs+优先队列
D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...
随机推荐
- Animations功能(区别于Transitions)
CSS3实现动画: 1 Transitions:定义元素的某个属性从一个属性值平滑过渡到另一个属性值. Transitions属性的使用方法如下所示: transition: property | ...
- Android 学习第6课,循环功能
package ch02; public class jiujiuchengfa { public static void main(String[] args) { // TODO 自动生成的方法存 ...
- Python 调用百度翻译API
由于实习公司这边做的是日文app,有时要看看用户反馈,对于我这种五十音图都没记住的人,表示百度翻译确实还可以.但不想每次都复制粘贴啊,google被墙也是挺蛋疼的事,所以用python结合baidu ...
- web移动端input获得光标Fixed定位失效解决方案
移动端业务开发,iOS 下经常会有 fixed 元素和输入框(input 元素)同时存在的情况. 但是 fixed元素在有软键盘唤起的情况下,会出现许多莫名其妙的问题. 这篇文章里就提供一个简单的有输 ...
- php开发工具。。
看了好多决定用phpstorm. hahaha PHP还是挺好玩的 但是貌似犯蠢一下,MAC自带有php环境: 我还下了一个XAMPP,不过无所谓啦. 都可以用
- bootstrap-13
bootstrap框架中制作导航条主要通过“.nav”样式.默认的.nav样式不提供默认的导航方式,必须附加另外一个样式才会有效,比如.nav-tabs,.nav-pill之类. 导航(标签型导航): ...
- oracle 常用语法
一.ORACLE的启动和关闭1.在单机环境下要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下su - oraclea.启动ORACLE系统oracle>svrmgrlSVRMG ...
- EXTJS信息提示框的注意事项
1.申明html:弹出框不完整 申明xhtml 2.当非必须参数不需要设定,而后续需要设置参数时,可设置为null. Ext.onReady(){ function(){ Ext.Message.pr ...
- C# 给某个方法设定执行超时时间 C#如何控制方法的执行时间,超时则强制退出方法执行 C#函数运行超时则终止执行(任意参数类型及参数个数通用版)
我自己写的 /// <summary> /// 函数运行超时则终止执行(超时则返回true,否则返回false) /// </summary> /// <typepara ...
- ACM_ICPC hdu-2111(简单贪心算法)
一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...