题意: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. 更改maven下载jar的仓库为阿里云仓库

    修改settings.xml <!-- 配置本地maven的仓库 --> <localRepository>D:\file\path\maven\repository</ ...

  2. Python之filter函数

    描述 filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表. 该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 Tru ...

  3. Eclipse中Ctrl+Shift+f快捷键无效的解决方式

    某天突然发现idea非常重要的快捷键ctrl+shift+f无效了,网上搜了很多都说是qq快捷键冲突,但是找了下qq快捷键却没有解决,现在给大家一个解决快捷键冲突的思路: 1.查看QQ快捷键--> ...

  4. CPU Meltdown和Spectre漏洞分析

    一.背景: 1月4日,国外爆出了整个一代处理器都存在的灾难性漏洞:Meltdown和Spectre. 几乎影响了全球20年内所有cpu处理器:这两个漏洞可以使攻击者通过利用并行运行进程的方式来破坏处理 ...

  5. delphi XE3解析JSON数据

    测试数据如下: Memo1.text中的数据: { "date":"周二(今天, 实时:12℃)", "dayPictureUrl":&qu ...

  6. 吴裕雄 数据挖掘与分析案例实战(8)——Logistic回归分类模型

    import numpy as npimport pandas as pdimport matplotlib.pyplot as plt # 自定义绘制ks曲线的函数def plot_ks(y_tes ...

  7. eclipse新建maven项目出现红叉解决办法

    新建的maven项目,项目内代码及pom.xml没有任何问题,但项目上就是有红叉,这时点开Markers(Window–>show veiw–>Markers),查看错误的详细信息,信息上 ...

  8. 在java中使用ffmpeg将amr格式的语音转为mp3格式

    ffmpeg是一个非常强大的音视频处理工具,官网是:http://ffmpeg.org/. 由于ffmpeg在windows上和linux系统上的执行文件不一样(Windows上不需要安装ffmpeg ...

  9. python的线程锁

    1.先看一个例子,一个进程可以起多个线程,多个线程都共享这个线程的内存 import threading import time num = 100 thread_lock = threading.L ...

  10. iOS - OC - JSON 解析 - NSJSONSerialization

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...