题目链接:

https://vjudge.net/problem/ZOJ-1649

题目大意:

天使的朋友要去救天使,a是天使,r 是朋友,x是卫兵。每走一步需要时间1,打倒卫兵需要另外的时间1,问救到天使所用的最少时间。注意存在救不到的情况。

思路:

BFS搜索,由于打倒卫兵时间为2,所以用BFS+优先队列做,每次出队时间最少的拓展,每个格子只走一次才是最优解

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<sstream>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 2e2 + ;
const int INF = 1e9 + ;
int T, n, m, cases;
int dir[][] = {,,,,-,,,-};
struct node
{
int x, y, time;
bool operator <(const node& a)const
{
return time > a.time;
}
node(){}
node(int x, int y, int time):x(x), y(y), time(time){}
};
char Map[maxn][maxn];
bool vis[maxn][maxn];
bool judge(int x, int y)
{
return (x >= && x < n && y >= && y < m && !vis[x][y] && Map[x][y] != '#');
}
void bfs(int x, int y)
{
memset(vis, , sizeof(vis));
priority_queue<node>q;
q.push(node(x, y, ));
vis[x][y] = ;
while(!q.empty())
{
node now = q.top();
q.pop();
if(Map[now.x][now.y] == 'r')
{
cout<<now.time<<endl;
return;
}
for(int i = ; i < ; i++)
{
node next = now;
next.x += dir[i][];
next.y += dir[i][];
if(judge(next.x, next.y))
{
vis[next.x][next.y] = ;
next.time++;
if(Map[next.x][next.y] == 'x')next.time++;
q.push(next);
}
}
}
printf("Poor ANGEL has to stay in the prison all his life.\n");
return;
}
int main()
{
while(cin >> n >> m)
{
int sx, sy;
for(int i = ; i < n; i++)
{
cin >> Map[i];
for(int j = ; j < m; j++)if(Map[i][j] == 'a')sx = i, sy = j;
}
bfs(sx, sy);
}
return ;
}

ZOJ-1649 Rescue---BFS+优先队列的更多相关文章

  1. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  2. ZOJ 1649 Rescue(有敌人迷宫BFS)

    题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过'x'方格须要两秒  '#'表示墙 因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...

  3. HDU1242 Rescue(BFS+优先队列)

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

  4. poj1649 Rescue(BFS+优先队列)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  5. hdu1242 Rescue bfs+优先队列

    直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...

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

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

  7. hdu1242 Rescue(BFS +优先队列 or BFS )

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:     Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...

  8. Rescue BFS+优先队列 杭电1242

    思路 : 优先队列 每次都取最小的时间,遇到了终点直接就输出 #include<iostream> #include<queue> #include<cstring> ...

  9. zoj 1649 Rescue

    BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...

  10. BFS zoj 1649

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...

随机推荐

  1. spring jpa 自定义查询数据库的某个字段

    spring jpa 提供的查询很强大, 就看你会不会用了. 先上代码, 后面在解释吧 1. 想查单个表的某个字段 在repository中 @Query(value = "select i ...

  2. 常用的Maven依赖

    一.数据库类型 1.mysql驱动 <!-- mysql驱动支持 --> <dependency> <groupId>mysql</groupId> & ...

  3. iOS开发-LayoutGuide(从top/bottom LayoutGuide到Safe Area)

    iOS7 topLayoutGuide/bottomLayoutGuide 创建一个叫做LayoutGuideStudy的工程,我们打开看一下Main.storyboard: storyboard-t ...

  4. 【Zabbix】大规模监控误报发生时的处理方案

    今天遇到了这样一件事..Zabbix一直在用的数据库突然间崩溃,无法连接了.在down掉的那一时刻开始,zabbix向管理员报了警.然后随之而来的是铺天盖地的所有主机zabbix agent无法连接的 ...

  5. windows上SSH服务连接远程主机失败

    putty连接出现错误提示"Network error:connection refused",xshell连接出现错误提示"connection failed:(por ...

  6. Android 动画 属性动画 视图动画 补间动画 帧动画 详解 使用

    Android动画 Property Animation res/animator/filename.xml In Java: R.animator.filename In XML: @[packag ...

  7. React Native 轻松集成统计功能(iOS 篇)

    最近产品让我加上数据统计功能,刚好极光官方支持数据统计 支持了 React Native 版本 第一步 安装: 在你的项目路径下执行命令: npm install janalytics-react-n ...

  8. python 面向对象的程序设计

    一:什么是编程范式? 编程是程序员用特定的语法 + 数据结构 + 算法组成的代码来告诉计算机如何执行任务的过程. 如果把编程的过程比喻为练习武功,那么编程范式指的就是武林中的各种流派,而在编程的世界里 ...

  9. 用C语言实现QQ刷屏

    我在百度传课上录制了一个用C语言实现刷屏的视频,有兴趣的可以移步这边:https://chuanke.baidu.com/6658388-238008.html

  10. WebSocket 聊天室加自制服务器

    自动监听本地ip 占用端口9930 打开服务器 再打开页面 输入服务器监听的ip和端口 局域网可以输入内网ip 外网连接 要输入服务器的外网ip 路由器需做好映射 实现WebSocket通信功能  和 ...