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 ...
随机推荐
- 【node】安装和配置node项目文件
需要把 views文件中的子文件全部改为以 .ejs的后缀 1·npm install express -g (全局安装) 2·npm install -g express-generator (安装 ...
- 【基础】这15种CSS居中的方式,你都用过哪几种?
简言 CSS居中是前端工程师经常要面对的问题,也是基本技能之一.今天有时间把CSS居中的方案汇编整理了一下,目前包括水平居中,垂直居中及水平垂直居中方案共15种.如有漏掉的,还会陆续的补充进来,算做是 ...
- python统计词频
arr = [1,2,3,4,5,6,4,5,2,3,6,8,9,6,5,3,6,2,4]dic={}for item in arr: if item in dic.keys(): dic[item] ...
- Dockerfile 指令 VOLUME 介绍
在介绍VOLUME指令之前,我们来看下如下场景需求: 1)容器是基于镜像创建的,最后的容器文件系统包括镜像的只读层+可写层,容器中的进程操作的数据持久化都是保存在容器的可写层上.一旦容器删除后,这些数 ...
- 【Python】 linux中python命令的命令行参数
Python命令行参数 原文地址:http://blog.163.com/weak_time/blog/static/25852809120169333247925/ Python的命令行参数,提供了 ...
- 【Python】 高级文件操作 shutil
shutil 很多时候,我想要对文件进行重命名,删除,创建等操作的时候的想法就是用subprocess开一个子进程来处理,但是实际上shutil可以更加方便地提供os的文件操作接口,从而可以一条语句搞 ...
- STL --> vector向量
vector向量 vector是一种对象实体,能够容纳许多其他类型相同的元素,因为又被称为容器. 头文件 在使用它时,需要包含头文件 <vector>. #include <vect ...
- PHP-CGI,FASTcgi,php-fpm,之间的关系?
刚开始对这个问题我也挺纠结的,看了<HTTP权威指南>后,感觉清晰了不少.首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. ...
- 开始补习JavaScript的第一天
JavaScript介绍: ①.JavaScript是一种解释性的,基于对象的脚本语言. ②.JavaScript是一种轻量级的编程语言,可以嵌入到html页面中,由浏览器来解释执行. ③.JavaS ...
- JVM学习九:JVM之GC算法和种类
我们前面说到了JVM的常用的配置参数,其中就涉及了GC相关的知识,趁热打铁,我们今天就学习下GC的算法有哪些,种类又有哪些,让我们进一步的认识GC这个神奇的东西,帮助我们解决了C 一直挺头疼的内存回收 ...