ZOJ-1649 Rescue---BFS+优先队列
题目链接:
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+优先队列的更多相关文章
- zoj 1649 Rescue (BFS)(转载)
又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...
- ZOJ 1649 Rescue(有敌人迷宫BFS)
题意 求迷宫中从a的位置到r的位置须要的最少时间 经过'.'方格须要1s 经过'x'方格须要两秒 '#'表示墙 因为有1s和2s两种情况 须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...
- HDU1242 Rescue(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- poj1649 Rescue(BFS+优先队列)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- hdu1242 Rescue bfs+优先队列
直接把Angle的位置作为起点,广度优先搜索即可,这题不是步数最少,而是time最少,就把以time作为衡量标准,加入优先队列,队首就是当前time最少的.遇到Angle的朋友就退出.只需15ms A ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- hdu1242 Rescue(BFS +优先队列 or BFS )
http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意: Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...
- Rescue BFS+优先队列 杭电1242
思路 : 优先队列 每次都取最小的时间,遇到了终点直接就输出 #include<iostream> #include<queue> #include<cstring> ...
- zoj 1649 Rescue
BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...
- BFS zoj 1649
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...
随机推荐
- 笔记:MyBatis XML配置-typeHandlers 默认类型处理器
类型处理器 Java 类型 JDBC 类型 BooleanTypeHandler java.lang.Boolean, boolean 数据库兼容的 BOOLEAN ByteTypeHandler j ...
- Day3---------Linux操作系统
---恢复内容开始--- 网络基础和DOS命令 一.网络分类 1.地理位置 1).局域网(LAN) 2).城域网(MAN) 3).广域网(WAN) 2.传输介质 1).有线网 2).光纤网 3).无线 ...
- 【Flask】 利用uWSGI和Nginx发布Flask应用
因为Flask比较容易上手,之前也拿flask写过几个小项目,不过当时天真地以为只要在服务器上nohup跑一个python脚本就算是成功发布了这个flask项目.实际上这还面临很多问题,比如并发性不好 ...
- 大数据 --> CAP原理和最终一致性
CAP原理和最终一致性 CAP原理和最终一致性(Eventually Consistency)
- 关于JAVA开发工具IDEA使用
安装IntelliJ IDEA 一.安装JDK 1 下载最新的jdk,这里下的是jdk-8u66 2 将jdk安装到默认的路径C:\Program Files\Java目录下 二.安装IntelliJ ...
- .NET Core初体验 在window上构建第一个app
ASP.NET Core 是一个跨平台,高性能的开源框架,用于构建现代化的,基于云的互联网应用程序.使用 ASP.NET Core ,您可以: 构建Web应用程序和服务,IoT应用程序和移动后端. 在 ...
- swift textview禁止用户使用复制粘贴
//自定义一个TextView class Own_TextView: UITextView { override func caretRect(for position: UITextPositio ...
- 关于DLL的创建与使用简单描述(C++、C#)
前言 前一段时间在学关于DLL的创建与调用,结果发现网络上一大堆别人分享的经验都有点问题.现在整理分享一下自己的方法. 工具 Microsoft Visual Studio 2017 depends ...
- Linux kernel 的 sendfile 是如何提高性能的
Linux kernel 的 sendfile 是如何提高性能的 现在流行的 web 服务器里面都提供 sendfile 选项用来提高服务器性能,那到底 sendfile 是什么,怎么影响性能的呢? ...
- 使用 memoryview 和 struct 查看一个 GIF 图像的首部
>>> import struct >>> fmt = '<3s3sHH' # ➊ >>> with open('filter.gif', ...