题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间。

析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条件——时间,所以我们用优先队列去优先获取时间短的路径,总体实现起来没有太大难度。

代码如下:

#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <iomanip>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <map>
#include <list> using namespace std;
const int maxn = 200 + 10;
const int dr[] = {0, 0, 1, -1};
const int dc[] = {1, -1, 0, 0};
struct node{
int r, c, t, d;
node() { }
node(int rr, int cc, int tt, int dd) : r(rr), c(cc), t(tt), d(dd) { }
bool operator < (const node &p) const {
if(t != p.t) return t > p.t;
return d > p.d;
}
}; char a[maxn][maxn];
node s, e;
int r, c, d[maxn][maxn]; bool is_in(int rr, int cc){
return rr < r && rr >= 0 && cc >= 0 && cc < c;
} void bfs(){
priority_queue<node> q; memset(d, -1, sizeof(d));
d[s.r][s.c] = 0;
q.push(s);
while(!q.empty()){
node u = q.top(); q.pop(); if(u.r == e.r && u.c == e.c){ printf("%d\n", u.t); return ; }
for(int i = 0; i < 4; ++i){
int x = u.r + dr[i];
int y = u.c + dc[i];
if(is_in(x, y) && a[x][y] == '.' && d[x][y] < 0){
d[x][y] = 1 + d[u.r][u.c];
q.push(node(x, y, u.t+1, u.d+1));
}
else if(is_in(x, y) && a[x][y] == 'x' && d[x][y] < 0){
d[x][y] = 1;
q.push(node(x, y, u.t+2, u.d+1));
}
}
} printf("Poor ANGEL has to stay in the prison all his life.\n");
return ;
} int main(){
while(~scanf("%d %d", &r, &c)){
for(int i = 0; i < r; ++i)
scanf("%s", a[i]); for(int i = 0; i < r; ++i)
for(int j = 0; j < c; ++j)
if(a[i][j] == 'r') { s.r = i; s.c = j; s.t = 0; s.d = 0; }
else if(a[i][j] == 'a') { e.r = i; e.c = j; e.t = 0; a[i][j] = '.'; }
bfs();
}
return 0;
}

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

  1. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  2. HDU 1242 Rescue(优先队列)

    题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description   Angel was caught by ...

  3. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  4. hdu 1242 Rescue (BFS)

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

  5. HDU 1242 rescue (优先队列模板题)

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

  6. HDU 1242——Rescue(优先队列)

    题意: 一个天使a被关在迷宫里,她的很多小伙伴r打算去救她.求小伙伴就到她须要的最小时间.在迷宫里有守卫.打败守卫须要一个单位时间.假设碰到守卫必须要杀死他 思路: 天使仅仅有一个,她的小伙伴有非常多 ...

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

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

  8. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  9. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  10. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

随机推荐

  1. fiddler 修改request请求

    例:在request url后追加&test=1参数 在OnBeforeRequest函数中添加以下代码 if(oSession.uriContains("www.bing.com/ ...

  2. vue 解决报错1

    [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available ...

  3. 专业英语词汇(Java)

    abstract (关键字)             抽象 ['.bstr.kt] access                            vt.访问,存取 ['.kses]‘(n.入口, ...

  4. MIUI添加内存调试工具:查看进程中的Bitmap信息

    Android开发中的内存管理一直是令人头痛的事情.其中占用内存最大的一般是Bitmap.   在上周五发布的MIUI开发版中,我添加了查看内存里Bitmap信息的功能.大家开发app的时候可以使用这 ...

  5. Hbase—学习笔记(一)

    此文的目的: 1.重点理解Hbase的整体工作机制 2.熟悉编程api,能够用来写程序 1.  什么是HBASE 1.1.   概念特性 HBASE是一个数据库----可以提供数据的实时随机读写 HB ...

  6. ubuntu 下当前网速查看

    ubuntu下用ethstatus可以监控实时的网卡带宽占用.这个软件能显示当前网卡的 RX 和 TX 速率,单位是Byte 一.安装 ethstatus 软件 #sudo apt-get insta ...

  7. cin中函数的作用

    cin是istream类的对象,它是从标准输入设备(键盘)获取数据,程序中的变量通过流提取符">>"从流中提取数据.流提取符">>"从流 ...

  8. 判断Javascript对象是否为空

    判断普通javascript对象是否为空(含有可枚举的属性,自有的.继承的都可以),可使用jQuery 3.2.1版的isEmptyObject()方法: isEmptyObject: functio ...

  9. windows系统如何真正隐藏文件夹[转载]

    方法一(推荐)eg:现需隐藏e盘bak目录下的tools文件夹e:\bak\tools运行:cmd键入:attrib +s +a +h +r e:\bak\tools然后,你再进去看e盘bak目录下, ...

  10. 修改mysql数据库 允许远程访问

    首先使用root身份进入MySQL mysql>use mysql; //进入mysql表 mysql> selecthost,user from user;  //查看结果是不是root ...