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
struct node {
{ friend bool operator> (node n1, node n2)
    friend bool operator> (node n1, node n2) {
    { return n1.priority > n2.priority;
        return n1.priority > n2.priority; }
    } int priority;
    int priority; int value;
    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点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ... 
随机推荐
- [转]概率基础和R语言
			概率基础和R语言 R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在小众领域闪耀着光芒.直到大数据的爆发,R语 ... 
- Golang  Clearing slice
			//first method : slice = nil // second method : slice = slice[0:0] Source page : https://www.socketl ... 
- ubuntu15.10跑裸机程序跑.bin文件
			1:安装tftp:#apt-get update#apt-get install tftp-hpa tftpd-hpa xinetd2:#cd /srv#mkdir tftp#chmod 777 tf ... 
- Node.js高级编程读书笔记 - 4 构建Web应用程序
			Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ... 
- 【 D3.js 入门系列 --- 2.1 】 关于如何选择,插入,删除元素
			在D3.js中,选择元素的函数有两个:select 和 selectAll . 先说明一下它们的区别: select 是选择所有指定元素的第一个 selectAll 是选择指定元素的全部(以用于后面同 ... 
- SEL方法选择器
			在Objective-C中,选择器(selector)有两个意思. 一是指在代码中的方法的名称.二是指在编译是用于替换方法名称的唯一的标识符.编译后的选择器的为SEL类型.所有名称相同的方法拥有同一个 ... 
- MSP430设置串口波特率的方法
			给定一个BRCLK时钟源,波特率用来决定需要分频的因子N: N = fBRCLK/Baudrate 分频因子N通常是非整数值,因此至少一个分频器和一个调制阶段用来尽可能的接 ... 
- Extjs各版本的下载链接
			Extjs的版本繁多,本文收集了Extjs各个版本的下载链接,包括官网和非官网的,以及各种汉化版api,欢迎大家下载分享. Extjs最新版下载链接:http://www.sencha.com/pro ... 
- apache-flume-1.5.0-bin windows
			1 testconsole.conf syslog-agent.sources = Syslogsyslog-agent.channels = MemoryChannel-1syslog-agent ... 
- mybatis 小于号 转义
			AND lbaq.watch_answer_start_datetime >= #{stm}AND lbaq.watch_answer_end_datetime <= #{etm} 此时报 ... 
