zoukankan      html  css  js  c++  java
  • 北京2018网络赛 hihocoder#1828 : Saving Tang Monk II (BFS + DP +多开一维)

    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 << "
    ";
    #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 '
    '
    
    #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;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;       
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    ll mod = 10007;
    const double esp = 1e-8;
    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=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    
    
    /*-----------------------showtime----------------------*/
                const int maxn = 209;
                int n,m;
                int sx,sy,ex,ey;
                char mp[maxn][maxn];
                int dp[maxn][maxn][10],vis[maxn][maxn];
                int nx[4][4] = {
                    {1,0},{0,1},{-1,0},{0,-1}
                };
                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=0; i<n; i++){
                        scanf("%s", mp[i]);
                        for(int j=0; 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,0));
                    dp[sx][sy][0]=0;
                    while(!que.empty()){
                        state t = que.front();
                        que.pop();
                        for(int i=0; i<4; i++){
                            int px = t.x + nx[i][0];
                            int py = t.y + nx[i][1];
                            if(px<0||py<0||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]+1){
                                        dp[px][py][t.cnt] =  dp[t.x][t.y][t.cnt]+1;
                                        que.push(state(px,py,t.cnt));
                                    }
                            }
                            else if(mp[px][py] == '#'){
                                if(t.cnt&&dp[px][py][t.cnt-1] > dp[t.x][t.y][t.cnt]+2){
                                        dp[px][py][t.cnt-1] =  dp[t.x][t.y][t.cnt]+2;
                                        que.push(state(px,py,t.cnt-1));
                                }
                            }
                            else if(mp[px][py] == 'B'){
                                if(t.cnt<5 && dp[px][py][t.cnt+1] > dp[t.x][t.y][t.cnt]+1){
                                        dp[px][py][t.cnt+1] =  dp[t.x][t.y][t.cnt]+1;
                                        que.push(state(px,py,t.cnt+1));
                                }
                                else if(t.cnt == 5 && dp[px][py][t.cnt] > dp[t.x][t.y][t.cnt]+1){
                                        dp[px][py][t.cnt] =  dp[t.x][t.y][t.cnt]+1;
                                        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]+1){
                                    dp[px][py][t.cnt] = dp[t.x][t.y][t.cnt]+1;
                                }
                            }
                            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=0; i<=5; i++){
                        ans = min(ans, dp[ex][ey][i]);
                    }
                    if(ans < inf)printf("%d
    ", ans);
                    else puts("-1");
                }
                return 0;
    }
    hihocoder 1828
  • 相关阅读:
    软件工程的实践项目的自我目标
    transform使用导致元素内字体出现模糊的坑~~~
    nvmw安装,用于控制node版本;
    开章大吉~
    eclipse运行Android项目出现“The connection to adb is down, and a severe error has occured. You must restart adb and Eclipse. ”
    Date对象相关函数使用
    Balsamiq Mockups 注册码
    如何关闭sublime更新提示
    如何在边框中加入文字
    如何用手机测试移动端页面
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/9720444.html
Copyright © 2011-2022 走看看