zoukankan      html  css  js  c++  java
  • 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[201][201];
    bool flag[201][201];
    int dx[]={1,0,-1,0};
    int dy[]={0,1,0,-1};
    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=0;
        q.push(now);
        flag[now.y][now.x]=true;
        
        while(!q.empty())
        {
            now=q.top();
            for(i=0;i<4;i++)
            {
                next.x=now.x+dx[i];
                next.y=now.y+dy[i];
                if(next.x>=1&&next.x<=m&&next.y>=1&&next.y<=n&&map[next.y][next.x]!='#'&&flag[next.y][next.x]==false)
                {
                    flag[next.y][next.x]=true;
                    next.time=now.time+1;
                    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 -1;
    }
    int main()
    {
        int i,j,xe,ye;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            vector<node> r;
            r.clear();
            for(i=1;i<=n;i++)
            {
                getchar();
                for(j=1;j<=m;j++)
                {
                    scanf("%c",&map[i][j]);
                }
            }
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=m;j++)
                {
                    if(map[i][j]=='r')
                    {
                        node temp;
                        temp.y=i;
                        temp.x=j;
                        temp.time=0;
                        r.push_back(temp);  
                    }
                }
            }
            
            int min=99999;
            for(i=0;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<0||r.size()==0)   //要判断是否有r,之前没判断,WA了几次
                printf("Poor ANGEL has to stay in the prison all his life.\n");
            else
                printf("%d\n",min);
        }
        return 0;
    }
    View Code

    这个是转得网上的 :

    在优先队列中,优先级高的元素先出队列。
    标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
    优先队列的第一种用法,也是最常用的用法:

    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
    {
        friend bool operator> (node n1, node n2)
        {
            return n1.priority > n2.priority;
        }
        int priority;
        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大小从小到大排序).

  • 相关阅读:
    LINUX查看进程开始时间、结束时间、运行时间
    excel字符处理函数
    oracle RMAN参数配置详解
    Linux添加双IP
    免费软电话 — XLite 的安装及配置向导
    Asterisk实现寻呼对讲广播的Page()命令详解
    自动化工具的重要性
    负载均衡之应用请求路由模块的使用(ARR)(七)[使用ARR来实现主机名共享]
    负载均衡之应用请求路由模块的使用(ARR)(二)[安装]
    需求管理随笔
  • 原文地址:https://www.cnblogs.com/chiry/p/3251797.html
Copyright © 2011-2022 走看看