hdu1242Rescue



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.
");
        else
            printf("%d
",ans);
    }
    return 0;
}






原文地址:https://www.cnblogs.com/nyist-xsk/p/7264926.html