题意: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. chrome 设置是否缓存

    在进行本地开发时,因老需要修改js,css等文件,而页面又带有缓存因此无法自动更新为新的文件. 在页面点击 -> F12 ->F1 ->References -> NetWor ...

  2. fiddler 修改request请求

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

  3. webserive学习记录1-jdk自带webservice

    最近在看webservice有视频,想年后找工作时增加点资本,视频终于看完了,自己又增加了些东西,现在就把视频中学到的和自己发现的东西总结一下. java jdk中自带一个轻量级的webservice ...

  4. 本地Facts

    我们可以通过Facts来获取目标主机的系统信息,当这些信息还不能满足我们的功能需要时,可以通过编写自定义的Facts模块来实现.当然,还有一个更简单的实现方法,就是通过本地Facts来实现.只需在目标 ...

  5. spring中作用域的问题

    在一般情况下:我们登录系统时,第一次登录当用户名或密码输入错误,在登录页面给出错误原因,当我们再刷新登录页面,应该是首次登录系统的页面(这时就不应该再给出出错提示),这时我们就应该想到错误提示信息到底 ...

  6. Andriod Studio adb 安装应用

    原文链接:https://blog.csdn.net/u014608640/article/details/51833304 下面的命令安装.重新安装和卸载应用程序. 安装:adb -s HT9BYL ...

  7. MySql log_bin

    [MySql log_bin] 1.查看 log_bin 是否启用. 默认情况下,mysql server 不启用 binlog(验证方法1: 执行"show variables" ...

  8. ASP.NET中的URL编码解码(转)

    在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗?测试: string file="文件上(传)篇.doc";string Server_UrlEncode=Server ...

  9. 36. Valid Sudoku (Array; HashTable)

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  10. Oracle 数据库的绑定变量特性及应用

    Oracle 数据库的绑定变量特性及应用[-----]转载自https://www.cnblogs.com/rootq/(原地址) 关键词: 绑定变量(binding variable),共享池(sh ...