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. 函数嵌套>作用域>闭包函数

    一:函数对象 函数是第一类对象,即表示函数可以当做数据传递 可以被引用:把函数内存地址赋值给一个变量名,仍然遵循函数的调用规则. 可以被当做参数传递:传递的是函数的运行的结果#可以当做返回值 把函数作 ...

  2. css3新增的属性

    由于CSS5标准还未完全订下来,所以各种内核的浏览器都有自己的标准,为了不使属性混淆,所以各家在各自标准前加了一个前缀, 如:-moz-       firefox火狐 -ms-         IE ...

  3. 安卓微信、QQ自带浏览器 UXSS 漏洞

    安卓微信.QQ自带浏览器 UXSS 漏洞 注:PDF报告原文下载链接 Author: hei@knownsec.com Date: 2016-02-29 一.漏洞描述 在安卓平台上的微信及QQ自带浏览 ...

  4. 【记录】尝试用QEMU模拟ARM开发板去加载并运行Uboot,kernel,rootfs【转】

    转自:https://www.crifan.com/try_use_qemu_emulate_arm_board_to_load_and_run_uboot_kernel_rootfs/ [背景] 手 ...

  5. MySQL参数设置

    InnoDB配置 从MySQL 5.5版本开始,InnoDB就是默认的存储引擎并且它比任何其它存储引擎的使用要多得多.那也是为什么它需要小心配置的原因. 1 innodb_file_per_table ...

  6. [shell]shell中if语句的使用

    转自:http://lovelace.blog.51cto.com/1028430/1211353 bash中如何实现条件判断?条件测试类型:    整数测试    字符测试    文件测试 一.条件 ...

  7. 关于一些问题的解决办法[记录]TF400017

    这个问题是今天在改东西的时候,突然断电导致的,google了很久之后,终于找到了办法 方法: 就是删除下面这个文件 -========================================= ...

  8. 【轨迹动画css】不规则轨迹动画css教程,弹球,客服广告悬浮层都可以用

    小demo如下,可更具自己需求修改: css @keyframes animX{ 0% {left: 0px;} 100% {left: 500px;} } @keyframes animY{ 0% ...

  9. Effective STL 笔记 -- Item 6 ~ 7: Container and Object Pointer

    Effective STL 笔记 – Item 6 ~ 7: Container and Object Pointer 中间两次笔记被删掉了,简单补一下: Item 3 中提到如果将对象直接放入容器中 ...

  10. LoadRunner参数化取值与连接数据库

    LoadRunner参数化取值与连接数据库   LoadRunner在使用参数化的时候,通常都是需要准备大数据量的,也因此LoadRunner提供两种参数化取值方式,一种是手动编辑,另一种就是通过连接 ...