結果

問題 No.3199 Key-Door Grid
ユーザー Today03
提出日時 2025-07-19 15:18:20
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 191 ms / 3,000 ms
コード長 2,497 bytes
コンパイル時間 3,264 ms
コンパイル使用メモリ 293,496 KB
実行使用メモリ 34,048 KB
最終ジャッジ日時 2025-07-19 15:18:29
合計ジャッジ時間 6,898 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
*/

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(),(x).end()
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)

template<typename T> int LB(const vector<T>& v, T x) { return lower_bound(ALL(v),x)-v.begin(); }
template<typename T> int UQ(T& v) { sort(ALL(v)); v.erase(unique(ALL(v)),v.end()); return v.size(); }
template<typename T> bool chmax(T &a, T b) { return a<b ? a=b, true : false; }
template<typename T> bool chmin(T &a, T b) { return a>b ? a=b, true : false; }
template<typename T> using rpriority_queue = priority_queue<T,vector<T>,greater<T>>;
using ll=long long; const int INF=1e9+10; const ll INFL=4e18;
using ld=long double; using lll=__int128_t; using ull=unsigned long long;
using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>;
using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>;


#ifdef LOCAL
#include "./debug.hpp"
#else
#define debug(...)
#define print_line
#endif


//----------------------------------------------------------

void solve() {
    ll H,W,M; cin>>H>>W>>M;
    VVI S(H,VI(W));
    PL start,goal;
    REP(i,H) REP(j,W) {
        char c; cin>>c;
        if(c=='S') start={i,j};
        if(c=='G') goal={i,j};
        if(c=='#') S[i][j]=-1;
        if('1'<=c && c<='9') S[i][j]=c-'0';
        if('a'<=c && c<='i') S[i][j]=101+(c-'a');
    }

    vector dp(H,VVL(W,VL(M+1,INF))); dp[start.first][start.second][0]=0;
    queue<array<ll,3>> que; que.push({start.first,start.second,0});

    while(!que.empty()) {
        auto [x,y,k]=que.front(); que.pop();

        for(auto [dx,dy]: {pair{0,1},{1,0},{0,-1},{-1,0}}) {
            ll nx=x+dx, ny=y+dy;
            if(nx<0 || nx>=H || ny<0 || ny>=W) continue;

            int v=S[nx][ny];
            if(v==-1) continue;

            if(v==0) {
                if(chmin(dp[nx][ny][k],dp[x][y][k]+1)) que.push({nx,ny,k});
            }
            if(v<=M) {
                if(chmin(dp[nx][ny][v],dp[x][y][k]+1)) que.push({nx,ny,v});
            }
            if(v>=100) {
                v-=100;
                if(v==k) {
                    if(chmin(dp[nx][ny][k],dp[x][y][k]+1)) que.push({nx,ny,k});
                }
            }
        }
    }

    ll ans=INF;
    REP(i,M+1) chmin(ans,dp[goal.first][goal.second][i]);
    if(ans==INF) ans=-1;
    cout<<ans<<endl;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    //cout<<fixed<<setprecision(15);
    int T=1;
    //cin>>T;
    while(T--) solve();
}
0