STL容器之优先队列

优先级队列,以前刷题的时候用的比较熟,现在竟然我只能记得它的关键字是priority_queue(太伤了)。在一些定义了权重的地方这个数据结构是很有用的。

先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部。priority_queue特别之处在于,允许用户为队列中存储的元素设置优先级。这种队列不是直接将新元素放置在队列尾部,而是放在比它优先级低的元素前面。标准库默认使用<操作符来确定对象之间的优先级关系,所以如果要使用自定义对象,需要重载 < 操作符。

优先队列有两种,一种是最大优先队列;一种是最小优先队列;每次取自队列的第一个元素分别是优先级最大和优先级最小的元素。

1) 优先队列的定义

包含头文件:"queue.h", "functional.h"

可以使用具有默认优先级的已有数据结构;也可以再定义优先队列的时候传入自定义的优先级比较对象;或者使用自定义对象(数据结构),但是必须重载好< 操作符。

2) 优先队列的常用操作

优先级队列支持的操作

q.empty()         如果队列为空,则返回true,否则返回false

q.size()            返回队列中元素的个数

q.pop()             删除队首元素,但不返回其值

q.top()             返回具有最高优先级的元素值,但不删除该元素

q.push(item)     在基于优先级的适当位置插入新元素

其中q.top()为查找操作,在最小优先队列中搜索优先权最小的元素,在最大优先队列中搜索优先权最大的元素。q.pop()为删除该元素。优先队列插入和删除元素的复杂度都是O(lgn),所以很快

另外,在优先队列中,元素可以具有相同的优先权。

H - Rescue

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Description

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. 



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

First line contains two integers stand for N and M. 



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

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 

Sample Input

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output

13
//本题从a开始搜索,搜到第一个r结束
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
char map[205][205];
int visit[205][205];
int dir[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
int n,m;
struct node
{
int x,y;
int time;
friend bool operator < (const node &a,const node &b)//操作符重载,时间小的先出队
{
return a.time>b.time;//优先队列要反着用
}
};
int go(int x,int y)
{
if(0<=x&&x<n&&0<=y&&y<m&&map[x][y]!='#')
return 1;
return 0;
}
int bfs(int x,int y)
{
int i;
node st,ed;
priority_queue<node>que; //定义一个优先队列
memset(visit,0,sizeof(visit));
st.x=x;
st.y=y;
st.time=0;
visit[st.x][st.y]=1;
que.push(st);
while(!que.empty())
{
st=que.top();//返回具有最高优先级的元素值,但不删除该元素
que.pop();//删除队首
if(map[st.x][st.y]=='r')
return st.time;
for(i=0; i<4; i++)
{
ed.x=st.x+dir[i][0];
ed.y=st.y+dir[i][1];
if(go(ed.x,ed.y)&&!visit[ed.x][ed.y])
{
visit[ed.x][ed.y]=1;
if(map[ed.x][ed.y]=='x')
ed.time=st.time+2;
else
ed.time=st.time+1;
que.push(ed);
}
}
}
return -1;
}
int main()
{
int i,j;
int x,y,ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0; i<n; i++)
scanf("%s",map[i]);
for(i=0; i<n; i++)
for(j=0; j<m; j++)
if(map[i][j]=='a')
{
x=i;
y=j;
break;
}
ans=bfs(x,y);
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
return 0;
}

hdu1242Rescue的更多相关文章

  1. BFS:HDU-1242-Rescue(带守卫的迷宫问题)(优先队列)

    解题心得: 1.读清楚题意,本题的题意是有多个'r'(起点),多个r多个bfs比较最短的时间即可,但是hdoj的数据比较水,直接一个起点就行了,迷宫里有多个守卫,如果在路途中遇到守卫会多花费一个时间点 ...

随机推荐

  1. laravel带条件查询手动分页

    后台php代码: //手动分页 $users = $kaoqin; //打算输出的数组,二维 $perPage = 10; if ($request->has('page')) { $curre ...

  2. 标准vim配置文件 带注释(适合C++编译)

    我的vim配置文件,适合大多数人习惯 """""""""""""&qu ...

  3. Linux日志文件/var/log详解

    更多内容推荐微信公众号,欢迎关注: 如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时 ...

  4. javascript 中的类数组和数组

    什么是类数组呢? 我们先来看一段代码: function fn() { console.dir(arguments); } fn(1,2,3,4,5,6,7,8,9,10); 这段代码的执行后,在 c ...

  5. $("节点名").html("字符串")和$("节点名").text("字符串")区别

    1. 经过html方法: $(".js_info").html("~!`@#$%^& ";'<>\=/-!·#¥%…&*()—+|` ...

  6. Dream------scala--类的属性和对象私有字段实战详解

    Scala类的属性和对象私有字段实战详解 一.类的属性 scala类的属性跟java有比较大的不同,需要注意的是对象的私有(private)字段 1.私有字段:字段必须初始化(当然即使不是私有字段也要 ...

  7. pytorch--cnn的理解

    class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.conv1 = nn.Conv2d(1, ...

  8. nodejs 接收上传的图片

    1.nodejs接收上传的图片主要是使用formidable模块,服务器是使用的express搭建. 引入formidable var formidable = require('./node_mod ...

  9. python基础--类的经典类vs新式类

    经典类VS新式类区别 1)写法新式类class Person(object):#new style 经典类class Persion: #classical style 2)调用父类 新式写法用sup ...

  10. Python模块制作

    在Python中,每个Python文件都可以作为一个模块,模块的名字就是文件的名字. 定义自己的模块 比如有这样一个文件test.py,在test.py中定义了函数add def add(a,b): ...