题意: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. AS3 巧用事件api简化鼠标拖动流程

     拖动,按照一般人的定义,拖动就是鼠标按下的时候移动鼠标,这里面有三个过程,分别是按下.移动鼠标和弹起.以stage为例,大家的实现步骤通常如下:(PS:此处不讨论startDrag和stopDrag ...

  2. ready 事件 DOM(文档对象模型) 已经加载....

    定义和用法 当 DOM(文档对象模型) 已经加载,并且页面(包括图像)已经完全呈现时,会发生 ready 事件. 由于该事件在文档就绪后发生,因此把所有其他的 jQuery 事件和函数置于该事件中是非 ...

  3. HttpClient post封装

    /** * @title HttpUtils * @description post请求封装 * @author maohuidong * @date 2017-12-18 */ public sta ...

  4. ubuntu upstart启动流程分析

    ubuntu自从6.10版本之后就使用了较新的upstart机制来进行系统的初始化. upstart是一种基于事件驱动的服务启动机制,可以使多个系统任务在保持依赖关系的前提下并发启动(据说这样这样启动 ...

  5. Object-c中的单例

    #import <UIKit/UIKit.h> @interface UniAudioPlayer:NSObject{ } +(UniAudioPlayer*) getInstance; ...

  6. jsp页面转发获取不到参数

    使用的是<input type="hidden" name="nameid" value="${nameid}"/>,隐藏默认值 ...

  7. Installation failed with message INSTALL_CANCELED_BY_USER.

    Installation failed with message INSTALL_CANCELED_BY_USER. It is possible that this issue is resolve ...

  8. SpringBoot application.yml logback.xml 多环境

    启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 java -jar app.j ...

  9. windows服务启动的进程无窗口

    勾选允许服务与桌面交互 指服务是否在桌面上提供用户界面,当服务启动后不论是谁登录都能使用.只有作为 LocalSystem 帐户(由“此帐户”指定)运行时,该选项才能使用. 如果一个服务需要界面(比如 ...

  10. 我在JS上解惑之路1

    1.为什么既然存在等号(==)非等号  (!=),又会有全等号(===)非全等号(!==)? *唯一的不同是后者判断时不进行类型转换. 例:var sNum = "66"; var ...