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. linux网卡驱动移植

    这里重要的是物理层PHY receiver,MAC(media access control)层,这里与软件中的协议栈不同,在硬件上MAC是PHY的下一层.DM9000A将MAC和PHY做到一起,也可 ...

  2. 动态添加Marquee标签,并动态赋值与属性

    前台加载js $(function(){ var publishStr="<%=publishText%>" var marqueeStr=" <mar ...

  3. Selenium定位一 --单个元素定位方法

    Selenium-Webdriver 提供了强大的元素定位方法,支持以下三种方法. 单个对象的定位方法 多个对象的定位方法 层级定位 定位单个元素在定位单个元素时,selenium-webdriver ...

  4. 转: MySQL 赋予用户权限(grant %-远程和localhost-本地区别)

    相关参考资料: MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. gr ...

  5. 淘宝ip库接口调用

    function ip($ip) {     $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;     $ipi ...

  6. Unity Shader——Writing Surface Shaders(1)——Surface Shader Examples

    这里有Surface Shader的一些例子.下面的这些例子关注使用内建的光照模型:关于如何使用自定义光照模型的例子参见Surface Shader Lighting Examples. 简单 我们将 ...

  7. 通过Oracle补充日志,找到锁阻塞源头的SQL

    问题背景: 有时会考虑一件事情,如果在Oracle环境下出现了锁阻塞的情况,如何定位到SQL源头(通过session.lock.transaction等视图仅能定位到会话)?或许有人会想有没有可能通过 ...

  8. js监听rem实现响应式

    原文链接:http://caibaojian.com/web-app-rem.html (function (doc, win) { var docEl = doc.documentElement, ...

  9. CSS 派生选择器

    派生选择器 通过依据元素在其位置的上下文关系来定义样式,你可以使标记更加简洁. 在 CSS1 中,通过这种方式来应用规则的选择器被称为上下文选择器 (contextual selectors),这是由 ...

  10. SQL 查询某个表被哪些存储过程使用到

    --1.查询某个表被哪些存储过程使用到 : select distinct object_name(id) from syscomments where id in (select object_id ...