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. 网摘关于BarCodeControl控件

    简介 BarCodeControl是一个用户制作条形码的控件. MicrosoftBarcodeControl9.0是可以在MicrosoftOfficeAccess窗体和报表中显示条码符号的Acti ...

  2. SpringBoot与异步任务、定时任务、邮件任务

    异步任务 在需要开启异步的服务加上注解:@Async @Service public class AsyncService { //告诉SpringBoot这是一个异步任务,SpringBoot会自动 ...

  3. URL访问 和命名规范

    手册:http://v9.help.phpcms.cn/html/2010/structure_0928/71.html http://yourdomain.com/index.php?m=conte ...

  4. Python练习-面向过程编程-模拟Grep命令

    其实这个面向过程编写程序,是编写程序的基础,所以一定要好好掌握 此程序涉及知识点:装饰器,生成器,协程器应用 # 编辑者:闫龙 import os Distinct = [] #定义一个列表用于判断重 ...

  5. 【译】第六篇 Replication:合并复制-发布

    本篇文章是SQL Server Replication系列的第六篇,详细内容请参考原文. 合并复制,类似于事务复制,包括一个发布服务器,一个分发服务器和一个或多个订阅服务器.每一个发布服务器上可以定义 ...

  6. Pycharm和IntelliJ IDEA激活 2017.3.x版本

    PyCharm 2017.3.3 (Professional Edition) 已经屏蔽了http://www.imsxm.com/   http-server激活方式,我根据参考教程搭建了一个,如有 ...

  7. 【codeforces】【比赛题解】#920 Educational CF Round 37

    [A]浇花 题意: 一个线段上每个整点都有花,有的点有自动浇花的喷水器,有问几秒能浇完所有的花. 题解: 大模拟 #include<cstdio> #include<cstring& ...

  8. tensorflow session 和 graph

    graph即tf.Graph(),session即tf.Session(),很多人经常将两者混淆,其实二者完全不是同一个东西. graph定义了计算方式,是一些加减乘除等运算的组合,类似于一个函数.它 ...

  9. (六)MyBatis杂项

    第一节:处理CLOB.BLOB类型数据 第二节:传入多个输入参数 第三节:MyBatis分页 1,逻辑分页 2,物理分页 MyBatis默认情况下,MyBatis启用一级缓存,即同一个SqlSessi ...

  10. SQL中EXCEPT和Not in的区别?

    初始化两张表: CREATE TABLE tb1(ID int) INSERT tb1          SELECT NULLUNION  ALL          SELECT NULLUNION ...