BFS,优先队列优化
题意:
'S' : 起点
'T' : 终点
'#' : 毒气室
'B' :氧气
'P':不消耗步数
每次经过毒气室需要一瓶氧气,氧气可以重复获得,但只能带五瓶氧气,问最少步数
solution:
HINT:多维状态判重,多一维携带氧气瓶数量
没带氧气瓶的时候不能走毒气室#
携带超过5个跳过氧气B
相似题目:UVA816 Abbott's Revenge这题多一维方向
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pw(x) (1ll << (x))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);++i)
#define per(i,l,r) for(int i=(r)-1;i>=(l);--i)
#define maxn 500005
#define eps 1e-9
#define PIE acos(-1)
#define dd(x) cout << #x << " = " << (x) << ", "
#define de(x) cout << #x << " = " << (x) << "\n"
#define endl "\n"
#define INF 0x3f3f3f3f
using namespace std;
typedef double db;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pii;
//----------------------
int n,m;
char pic[][];
bool vis[][][];
const int dir[][]={{-,},{,},{,},{,-}};
struct Node{
int x;
int y;
int d=;
int cnt=;
bool operator<(const Node& a)const{
return d>a.d;
}
}start,u,v;
int cnt;
bool check(Node v)
{
return v.x>=&&v.x<n&&v.y>=&&v.y<m;
}
int bfs(int x,int y)//把#当路障,找氧气瓶
{
memset(vis,,sizeof(vis));
priority_queue<Node>q;
vis[x][y][]=;
start.x=x;start.y=y;start.d=;start.cnt=;
q.push(start);
while(!q.empty())
{
u=q.top();q.pop();
if(pic[u.x][u.y]=='T'){return u.d;}
rep(i,,){
v=u;
v.x+=dir[i][];
v.y+=dir[i][];
if(!check(v))continue;
if(pic[v.x][v.y]=='#'){
if(v.cnt>=)v.cnt--,v.d++;
else continue;
}
else if(pic[v.x][v.y]=='B'){
if(v.cnt>=)continue;
else v.cnt++;
}
else if(pic[v.x][v.y]=='P')v.d--;
v.d++;
if(vis[v.x][v.y][v.cnt])continue;
vis[v.x][v.y][v.cnt]=;
// dd(v.x+1);dd(v.y+1);dd(pic[v.x][v.y]);dd(v.cnt);de(v.d);
q.push(v);
}
}
return INF;
}
int main()
{
// ifstream cin("in.txt");
while(cin>>n>>m,n+m){
int ans=INF;
int ok=;
rep(i,,n)rep(j,,m){
cin>>pic[i][j];
}
rep(i,,n)rep(j,,m)if(pic[i][j]=='S')
{
ans=bfs(i,j);
goto here;
}
here:;
if(ans!=INF)printf("%d\n",ans);
else puts("-1");
}
return ;
}
BFS,优先队列优化的更多相关文章
- HDU 6386 Age of Moyu 【BFS + 优先队列优化】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6386 Age of Moyu Time Limit: 5000/2500 MS (Java/Others ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- hdu-1026(bfs+优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:输入n,m和一个n*m的矩阵, .表示通路: x表示墙: n表示有一个怪物,消灭它需要n个 ...
- hdu1026(bfs+优先队列+打印路径)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- HDU6582 Path【优先队列优化最短路 + dinic最大流 == 最小割】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 来源:2019 Multi-University Training Contest 1 题目大意 ...
- dijkstra算法之优先队列优化
github地址:https://github.com/muzhailong/dijkstra-PriorityQueue 1.题目 分析与解题思路 dijkstra算法是典型的用来解决单源最短路径的 ...
- 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛
传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- 最短路算法模板合集(Dijkstar,Dijkstar(优先队列优化), 多源最短路Floyd)
再开始前我们先普及一下简单的图论知识 图的保存: 1.邻接矩阵. G[maxn][maxn]; 2.邻接表 邻接表我们有两种方式 (1)vector< Node > G[maxn]; 这个 ...
随机推荐
- ubuntu目录结构(转)
/:根目录,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中 /bin:/usr/bin:可执行二进制文件的目录,如常用的命令ls. ...
- HTML and CSS basis
classes 和 IDs 的不同 class 选择器用于描述一组元素的样式,class 选择器有别于id选择器,class可以在多个元素中使用. HTML 元素 elements 从开始标签(sta ...
- SqlServer 附加数据库出错
方法一 找到要添加数据库的.mdf文件,点击右键,选择属性 在属性页面点击安全,选择Authenticated Users,单击编辑 Authenticated Users权限中选择完全控制,点击确定 ...
- 通过ABAP代码判断当前系统类型,BYD还是S4 OP还是S4 Cloud
用工具类 CL_COS_UTILITIES IS_BYD 如果是BYD系统,这个方法的实现会硬编码返回一个true, 在其他系统里则返回false,如图: IS_SUITE 原理同上,suite系统里 ...
- Spring的启动流程
spring的启动是建筑在servlet容器之上的,所有web工程的初始位置就是web.xml,它配置了servlet的上下文(context)和监听器(Listener),下面就来看看web.xml ...
- MySQL cmd操作
1.开启关闭服务 net start mysql net stio mysql 2.登陆 在CMD命令窗口敲入命令 mysql -hlocalhost -uroot -p 后按回车(注意这里的&quo ...
- C 动态内存申请
例子: int *p=0; int number=0; scanf("%d",&number); p = (int*)malloc(number*sizeof(int));
- 牛客小白月赛19 E 「火」烈火燎原 (思维,树)
牛客小白月赛19 E 「火」烈火燎原 (思维,树) 链接:https://ac.nowcoder.com/acm/contest/2272/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空 ...
- MyBatis-09-Lombok
9.Lombok Project Lombok is a java library that automatically plugs into your editor and build tools, ...
- solr schema.xml配置
solr使用_version_来做文档的版本控制和修改时加锁(乐观锁) <field name="_version_" type="long" index ...