题意: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. opencv一些资料

    中文论坛: http://www.opencv.org.cn/forum.php?mod=forumdisplay&fid=1 yuv与mat转换: https://www.cnblogs.c ...

  2. 前端-javascript-BOM-浏览器对象模型

    BOM的介绍---浏览器对象模型. 操作浏览器部分功能的API.比如让浏览器自动滚动. -------------------------------------------------------- ...

  3. php, postgresql 安装

    sudo yum install postgresql84-server postgresql84-contrib ubuntu下面安装的问题解决: Postgresql installation o ...

  4. Data Guard 介绍

  5. 【348】通过 Numpy 创建各式各样的矩阵

    参考:NumPy之array-一个程序媛的自我修养-51CTO博客 参考:numpy中数组和矩阵的区别 - jiangsujiangjiang的博客 - CSDN博客 一.使用系统方法 二.用指定的数 ...

  6. 重学Java

    Java web 的课程告一段落了 现在我觉得我应该重新学习一下 Java基础  先分享下昨天学习递归后写的两个短短的代码 1.求5的阶乘 package test; public class fiv ...

  7. 查看dns节点的内存是否够用

    dmesg查看是否有报错

  8. MongoDB 分片副本集集群搭建

    配置准备 三台机器: A(193.168.10.101) B(193.168.10.102) C(193.168.10.103) MongoDB 安装目录:/usr/local/mongodb Mon ...

  9. django创建工程,用命令

    django创建工程的命令 >>python C:\Python33\Lib\site-packages\django\bin\django-admin.py startproject p ...

  10. 批量删除进程清理 minerd

    发现顽固minerd 进程与ntp一起启动,所以一起杀掉 yum remove ntp kill -9 `ps -ef | grep ntp|awk '{print $2}'` kill -9 `ps ...