hihocoder 1828 :https://hihocoder.com/problemset/problem/1828

学习参考:https://www.cnblogs.com/tobyw/p/9691431.html

题意:

    给定一个图中,让你回答从S点跑到T点的最短时间。“.”点是可以直接走上去,耗时+1,“P”加速点,就是不耗时就可以走上去,“#”毒气点,必须要有氧气瓶才能进入,且耗时+2,“B”是氧气瓶补给点,每次进去可以得到一个氧气瓶,但是你最多可以携带5个氧气瓶,耗时+1。

思路:

    我觉得这道题看到5个氧气瓶的限制是个关键。可以给每个点开5个空间(准确的说是6个,还有0的情况),这样就直接bfs转移就可以了。自己练习的时候一直没想到再多开一维。现在想想,感觉能看见和想到高维度的人都好神奇。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
ll mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = ;
int n,m;
int sx,sy,ex,ey;
char mp[maxn][maxn];
int dp[maxn][maxn][],vis[maxn][maxn];
int nx[][] = {
{,},{,},{-,},{,-}
};
struct state
{
int x,y,cnt; state(int x,int y,int s):x(x),y(y),cnt(s){}
};
int main(){
while(~scanf("%d%d", &n, &m) && n+m){
memset(dp,inf,sizeof(dp));
for(int i=; i<n; i++){
scanf("%s", mp[i]);
for(int j=; j<m; j++){
if(mp[i][j] == 'S')sx=i,sy=j;
if(mp[i][j] == 'T')ex=i,ey=j;
}
}
queue<state>que;
que.push(state(sx,sy,));
dp[sx][sy][]=;
while(!que.empty()){
state t = que.front();
que.pop();
for(int i=; i<; i++){
int px = t.x + nx[i][];
int py = t.y + nx[i][];
if(px<||py<||px>=n||py>=m)continue;
// vis[px][py] = 1;
if(mp[px][py]=='.' || mp[px][py] == 'S'){
if(dp[px][py][t.cnt] > dp[t.x][t.y][t.cnt]+){
dp[px][py][t.cnt] = dp[t.x][t.y][t.cnt]+;
que.push(state(px,py,t.cnt));
}
}
else if(mp[px][py] == '#'){
if(t.cnt&&dp[px][py][t.cnt-] > dp[t.x][t.y][t.cnt]+){
dp[px][py][t.cnt-] = dp[t.x][t.y][t.cnt]+;
que.push(state(px,py,t.cnt-));
}
}
else if(mp[px][py] == 'B'){
if(t.cnt< && dp[px][py][t.cnt+] > dp[t.x][t.y][t.cnt]+){
dp[px][py][t.cnt+] = dp[t.x][t.y][t.cnt]+;
que.push(state(px,py,t.cnt+));
}
else if(t.cnt == && dp[px][py][t.cnt] > dp[t.x][t.y][t.cnt]+){
dp[px][py][t.cnt] = dp[t.x][t.y][t.cnt]+;
que.push(state(px,py,t.cnt));
}
}
else if(mp[px][py] == 'T'){
if(dp[px][py][t.cnt] > dp[t.x][t.y][t.cnt]+){
dp[px][py][t.cnt] = dp[t.x][t.y][t.cnt]+;
}
}
else if(mp[px][py] == 'P'){
if(dp[px][py][t.cnt] > dp[t.x][t.y][t.cnt]){
dp[px][py][t.cnt] = dp[t.x][t.y][t.cnt];
que.push(state(px,py,t.cnt));
}
}
}
}
int ans = inf;
for(int i=; i<=; i++){
ans = min(ans, dp[ex][ey][i]);
}
if(ans < inf)printf("%d\n", ans);
else puts("-1");
}
return ;
}

hihocoder 1828

北京2018网络赛 hihocoder#1828 : Saving Tang Monk II (BFS + DP +多开一维)的更多相关文章

  1. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】

    任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...

  2. hihocoder #1828 : Saving Tang Monk II(BFS)

    描述 <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chi ...

  3. hihocoder 1828 Saving Tang Monk II (DP+BFS)

    题目链接 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great C ...

  4. ACM-ICPC 2018北京网络赛-A题 Saving Tang Monk II-优先队列

    做法:优先队列模板题,按步数从小到大为优先级,PASS掉曾经以相同氧气瓶走过的地方就好了 题目1 : Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制: ...

  5. Saving Tang Monk II(bfs+优先队列)

    Saving Tang Monk II https://hihocoder.com/problemset/problem/1828 时间限制:1000ms 单点时限:1000ms 内存限制:256MB ...

  6. hihoCoder-1828 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II BFS

    题面 题意:N*M的网格图里,有起点S,终点T,然后有'.'表示一般房间,'#'表示毒气房间,进入毒气房间要消耗一个氧气瓶,而且要多停留一分钟,'B'表示放氧气瓶的房间,每次进入可以获得一个氧气瓶,最 ...

  7. ACM-ICPC2018北京网络赛 Saving Tang Monk II(bfs+优先队列)

    题目1 : Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also < ...

  8. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  9. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

随机推荐

  1. Docker 安装部署Sql Server

    前言 在如今,容器化概念越来越盛行,.Net Core项目也可以跨平台部署了,那么思考下Sql Server能不能呢?当然是可以的啦.本文今天就是介绍Docker部署配置和连接Sql Server.本 ...

  2. 在Java大环境下.NET程序员如何夺得一线生机

    先来看一组数据,从某招聘网站直接检索3-4w的岗位,会看到Java与.NET社会需求量的巨大差异,这里就不再对比高薪的岗位了,.NET的高薪岗位更是少的可怜:   笔者从业十余年,一直是在.NET圈子 ...

  3. c#链接数据库,查找数据信息

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. sharding demo 读写分离 U (分库分表 & 不分库只分表)

    application-sharding.yml sharding: jdbc: datasource: names: ds0,ds1,dsx,dsy ds0: type: com.zaxxer.hi ...

  5. Hadoop学习(9)-spark的安装与简单使用

    spark和mapreduce差不多,都是一种计算引擎,spark相对于MapReduce来说,他的区别是,MapReduce会把计算结果放 在磁盘,spark把计算结果既放在磁盘中有放在内存中,ma ...

  6. 如何成为PHP程序员?

    当今,互联网的蓬勃发展,移动互联网的火热,以及国家提出的“互联网+”.这些趋势可以让我们明显的感觉到互联网的重要,不可替代.网站也是大家最早接触,最早认识的一种新事物.谈到网站,无非最长脸的莫过于PH ...

  7. Nightwatch——自动化测试(端对端e2e)

    背景: 前端页面模拟仿真操作,目的是避免每次更新相关内容重复之前的测试操作,减少不必要的时间投入,以及校验功能的可用性.但是目前元素定位是个问题(每次页面有修改都要重设某些元素定位) 测试分类: 一. ...

  8. React Native 混合开发与实现

    关于 微信公众号:前端呼啦圈(Love-FED) 我的博客:劳卜的博客 知乎专栏:前端呼啦圈 前言 随着 React 的盛行,其移动开发框架 React Native 也收到了广大开发者的青睐,以下简 ...

  9. Java 调用http接口(基于OkHttp的Http工具类方法示例)

    目录 Java 调用http接口(基于OkHttp的Http工具类方法示例) OkHttp3 MAVEN依赖 Http get操作示例 Http Post操作示例 Http 超时控制 工具类示例 Ja ...

  10. Javascript中将数字转换为中文的方法

    //js实现将数字1234转化为汉字字符串(一千二百三十四)(或大写汉字壹仟贰佰叁拾肆): /*阿拉伯数字转中文数字 中文数字的特点: 每个计数数字都跟着一个权位,权位有:十.百.千.万.亿. 以“万 ...