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

这个是转得网上的 :

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

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大小从小到大排序).

HDOJ(1242)BFS+优先队列的更多相关文章

  1. hdu 1242(BFS+优先队列)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  3. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  4. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  5. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  6. hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)

    题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...

  7. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  8. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  9. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  10. D. Lunar New Year and a Wander bfs+优先队列

    D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...

随机推荐

  1. Android 开发错误信息001

    Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessExceptio ...

  2. ubuntu 使用wine卸载软件

    现在的网络应用许多被windows绑架了,有时候不得不下载一些.exe的的东西在ubuntu下载配合一下,但是后来我想卸载这些.exe应用程序,于是百度了一下,一位博主写的好,如下,晒出来: cd ~ ...

  3. input file上传文件扩展名限制

    方法一(不推荐使用):用jS获获取扩展名进行验证: <script type="text/javascript" charset="utf-8"> ...

  4. UIButton的使用

    使用UIButton时需要注意的是: 1.UIButton的创建有专门的类方法(buttonWithType:,UILabel没有): 2.UIButton常用的属性包括:frame.titile.t ...

  5. Hello Dojo!(翻译)

    http://dojotoolkit.org/documentation/tutorials/1.10/hello_dojo/index.html 欢迎学习DOJO!在本教程中,你将学些到如何加载DO ...

  6. WebApi:使用方法名或者控制器名作为接口地址

    今天遇到一个问题:新建的WebApi的项目生成的接口的地址都是以控制器的名字命名的,这样的话,在方法前添加ActionName就不起作用了,但之前一个项目是可以的. 接口代码: public clas ...

  7. ubuntu14升级到15后遇到的问题

    ubuntu14的unity坏了,于是就找了个镜像升级到15.但是升级完android开发环境出了点问题.. 1:Picked up JAVA_TOOL_OPTIONS: -javaagent:/us ...

  8. 【巩固】JS获取时间的一些基础知识

    就是一个new Date()对象,要注意的有以下几点; 直接给oDate对象设置年月日时分秒的时候要分成两步,oDate.setFullYear()接受三个参数分别是年月日,注意月份是从0开始计一月的 ...

  9. ADF_Starting系列9_使用EJB/JPA/JSF通过ADF构建Web应用程序之测试J2EE Container

    2013-05-01 Created By BaoXinjian

  10. IO 输入流操作

    //get.h #ifndef GET_H #define GET_H #include <iostream> std::istream& get(std::istream& ...