【BFS+优先级队列】Rescue
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/I
【题意】
- 给定一个n*m的迷宫,A的多个小伙伴R要去营救A,问需要时间最少的小伙伴是多长时间
- 遇到'.'需要1分钟,遇到'x'需要两分钟,遇到'#'不能走
【思路】
- 这道题的关键是遇到'.'和遇到'x'需要的时间不一样,所以不能用平常的BFS找最短路
- vis数组不再是true和false两个状态来标记是否已经走过,而是记录最小步数的值
- 不再是vis是false就加入队列,而是vis小于最小步数加入队列
- 因为有多个R营救一个A,所以是以A为起点BFS
【Accepted】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue> using namespace std; int n,m;
const int maxn=2e2+;
const int inf=0x3f3f3f3f;
char str[maxn][maxn];
//bool vis[maxn][maxn];
int vis[maxn][maxn];
struct node
{
int x;
int y;
int step;
node(){}
node(int _x,int _y,int _s):x(_x),y(_y),step(_s){}
friend bool operator<(node nd1,node nd2)
{
return nd2.step<nd1.step;
}
}nd[maxn];
int dir[][]={{,},{-,},{,},{,-}};
bool check(int x,int y)
{
if(str[x][y]=='#') return false;
if(x<||x>=n||y<||y>=m) return false;
return true;
}
int BFS(int x,int y)
{
priority_queue<node> Q;
Q.push(node(x,y,));
vis[x][y]=;
while(!Q.empty())
{
node q=Q.top();
Q.pop();
if(str[q.x][q.y]=='r') return q.step;
for(int i=;i<;i++)
{
int x=q.x+dir[i][];
int y=q.y+dir[i][];
if(check(x,y))
{
int s;
if(str[x][y]=='x') s=q.step+;
else s=q.step+;
if(s<vis[x][y])
{
vis[x][y]=s;
Q.push(node(x,y,s));
}
}
}
}
return -;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,inf,sizeof(vis));
int x,y;
for(int i=;i<n;i++)
{
scanf("%s",str[i]);
for(int k=;k<m;k++)
{
if(str[i][k]=='a')
{
x=i;
y=k;
}
}
}
int ans=BFS(x,y);
if(ans==-)
{
puts("Poor ANGEL has to stay in the prison all his life.");
}
else
{
printf("%d\n",ans);
}
}
return ;
}
BFS+优先级队列
【BFS+优先级队列】Rescue的更多相关文章
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 算法与数据结构基础 - 堆(Heap)和优先级队列(Priority queue)
堆基础 堆(Heap)是具有这样性质的数据结构:1/完全二叉树 2/所有节点的值大于等于(或小于等于)子节点的值: 图片来源:这里 堆可以用数组存储,插入.删除会触发节点shift_down.shif ...
- 体验Rabbitmq强大的【优先级队列】之轻松面对现实业务场景
说到队列的话,大家一定不会陌生,但是扯到优先级队列的话,还是有一部分同学是不清楚的,可能是不知道怎么去实现吧,其实呢,,,这东西已 经烂大街了...很简单,用“堆”去实现的,在我们系统中有一个订单催付 ...
- Java中的队列Queue,优先级队列PriorityQueue
队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...
- 如何基于RabbitMQ实现优先级队列
概述 由于种种原因,RabbitMQ到目前为止,官方还没有实现优先级队列,只实现了Consumer的优先级处理. 但是,迫于种种原因,应用层面上又需要优先级队列,因此需求来了:如何为RabbitMQ加 ...
- ACM/ICPC 之 优先级队列+设置IO缓存区(TSH OJ-Schedule(任务调度))
一个裸的优先级队列(最大堆)题,但也有其他普通队列的做法.这道题我做了两天,结果发现是输入输出太过频繁,一直只能A掉55%的数据,其他都是TLE,如果将输入输出的数据放入缓存区,然后满区输出,可以将I ...
- java中PriorityQueue优先级队列使用方法
优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...
- stl的优先级队列
#include <iostream> #include <vector> #include <queue> using namespace std; class ...
- 【python cookbook】【数据结构与算法】5.实现优先级队列
问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素: 解决方案:采用heapq模块实现一个简单的优先级队列 # example.py # # Exam ...
随机推荐
- C/S WinForm自动升级
这二天刚好完成一个C/S 自动升级的功能 代码分享一下 /// <summary> /// 版本检测 /// </summary> public class ...
- JDK NIO SelectionKey bug
此bug项目中使用elasticSearch中出现的,原因是,nio事件选择器,在特性内核下以及jdk6版本中,出现不hold线程,死循环获取事件的bug,导致cup使用率过高: 此bug在官网已被修 ...
- random模块详解
1.import random random·randint(a,b) 括号里是一个范围,random·randint()是取括号里范围的随机数. >>> import random ...
- UI常用字体定义和继承的实例,ResearchKitCode
#import <UIKit/UIKit.h> @interface UIFont (APCAppearance) + (UIFont*) appRegularFontWithSize: ...
- 【HEVC帧间预测论文】P1.9 Coding Tree Depth Estimation for Complexity Reduction of HEVC
Coding Tree Depth Estimation for Complexity Reduction of HEVC <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见: ...
- Android从图库选择照片
从机里取照片,开头用网上找的代码测试,导致类似下面这样的Crash:java.lang.RuntimeException: Failure delivering result java.lang.Se ...
- [转] Figuring out why my SVCHOST.EXE is at 100% CPU without complicated tools in Windows 7
(转自:Figuring out why my SVCHOST.EXE is at 100% CPU without complicated tools in Windows 7 - Scott Ha ...
- uva1380 A Scheduling Problem
按紫书来注意这道题的题目给了很大的方便,就相当于验证k是不是答案,不是的话就是k+1 #include<iostream> #include<string> #include& ...
- python制作二维码
1.安装MyQR pip install MyQR 2.打开pycharm,新建项目文件 from MyQR import myqr 2.1黑白二维码 2.2,彩色动态二维码 3.当前项目本地 ...
- Deep_into_iris
具体ipynb文件请移步Github #各种所需要的库函数首先加载 import numpy as np import pandas as pd import matplotlib.pyplot as ...