题意:

'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,优先队列优化的更多相关文章

  1. 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 ...

  2. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  3. hdu-1026(bfs+优先队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:输入n,m和一个n*m的矩阵, .表示通路: x表示墙: n表示有一个怪物,消灭它需要n个 ...

  4. hdu1026(bfs+优先队列+打印路径)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  6. HDU6582 Path【优先队列优化最短路 + dinic最大流 == 最小割】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 来源:2019 Multi-University Training Contest 1 题目大意 ...

  7. dijkstra算法之优先队列优化

    github地址:https://github.com/muzhailong/dijkstra-PriorityQueue 1.题目 分析与解题思路 dijkstra算法是典型的用来解决单源最短路径的 ...

  8. 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛

    传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...

  9. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  10. 最短路算法模板合集(Dijkstar,Dijkstar(优先队列优化), 多源最短路Floyd)

    再开始前我们先普及一下简单的图论知识 图的保存: 1.邻接矩阵. G[maxn][maxn]; 2.邻接表 邻接表我们有两种方式 (1)vector< Node > G[maxn]; 这个 ...

随机推荐

  1. vue + echarts 实现中国地图 展示城市

    Demo 安装依赖 vue中安装echarts npm install echarts -S 在main.js中引用 import echarts from 'echarts'Vue.prototyp ...

  2. Maven新建项目出现 Could not calculate build plan:plugin 错误解决办法

    删除本地.m2仓库中 org.apache.maven.plugins:maven-resources-plugin所在目录. 然后右击项目 Maven->Update Project-> ...

  3. 前端基础(二):CSS

    CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...

  4. kubernetes之pod生命周期,pod重启策略, 镜像拉取策略

    pod声明周期(状态):pending , running, succeeded, failed, unknown 挂起(Pending):Pod 已被 Kubernetes 系统接受,但有一个或者多 ...

  5. VS---《在VS2010中 使用C++创建和使用DLL》(001)

    VS---<在VS2010中 使用C++创建和使用DLL>(001) 需要学习制作和使用动态库,现在知道:DLL调用有两种方式,一种是静态调用,另外一种是动态调用.详细的还不算明白,等后期 ...

  6. 8.caffe:make_mean.sh( 数据平均化 )

    个人实践代码如下: #!/usr/bin/env sh # Compute the mean image from the imagenet training lmdb # N.B. this is ...

  7. systemctl可以实现nginx进程挂了之后自动重新启动

    接 2018年7月31日的那篇: vim /lib/systemd/system/nginx.service [Service]Restart=alwaysRestartSec=1Type=forki ...

  8. golang type conversion

    map[string]interface{} is not the same as map[string]string. Type interface{} is not the same as typ ...

  9. connect: network is unreachable

    解决方法: 在确保完善网卡驱动,以及确保将网卡驱动编译进内核后,检查 ls /etc/sysconfig/network-script/ifcfg-eth0 一.看是否在上述目录下存在ifcfg-et ...

  10. osm2pgsql

    osm2pgsql -d gis --create --slim --drop --flat-nodes '/data/nodes.bin' -G --hstore --tag-transform-s ...