結果

問題 No.3199 Key-Door Grid
コンテスト
ユーザー 北杜
提出日時 2025-07-11 22:50:42
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 2,107 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,292 ms
コンパイル使用メモリ 391,428 KB
実行使用メモリ 17,536 KB
最終ジャッジ日時 2026-07-13 04:05:52
合計ジャッジ時間 6,919 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 17
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:36:9: warning: 'si' may be used uninitialized [-Wmaybe-uninitialized]
   36 |     int si, sj;
      |         ^~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_iterator.h:78,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_algobase.h:67,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/algorithm:62,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:53,
                 from main.cpp:2:
In function 'constexpr _Tp* std::construct_at(_Tp*, _Args&& ...) [with _Tp = iti; _Args = {const iti&}]',
    inlined from 'static constexpr void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = iti; _Args = {const iti&}; _Tp = iti]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/alloc_traits.h:676:21,
    inlined from 'void std::deque<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = iti; _Alloc = std::allocator<iti>]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_deque.h:1610:30,
    inlined from 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = iti; _Sequence = std::deque<iti, std::allocator<iti> >]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_queue.h:313:20,
    inlined from 'int main()' at main.cpp:43:11:
/home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_construct.h:110:16: warning: 'sj' may be used uninitialized [-Wmaybe-uninitialized]
  110 |         return ::new(__loc) _Tp(std::forward<_Args>(__args)...);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:36:13: note: 'sj' was declared here
   36 |     int si, sj;
      |             ^~

ソースコード

diff #
raw source code

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

#define rep(i, n) for (int i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
const int INF32 = 2e9;
const ll INF64 = 4e18;

const vector<int> dx = {1, 0, -1, 0},
                  dy = {0, 1, 0, -1};

void printYN(bool ok){
    if(ok)cout << "Yes" << endl;
    else cout << "No" << endl;
    return;
}

struct iti {
    int i, j, k;
};

int main() {
    int H, W, M;
    cin >> H >> W >> M;
    vector <string> S(H);
    rep(i, H)cin >> S[i];
    vector<vector<string>> G(M+1);
    rep(i, M+1){
        G[i] = S;
    }
    int si, sj;
    rep(i, H)rep(j, W)if(G[0][i][j]=='S'){
        si = i, sj = j;
    }
    queue<iti> q;
    iti st;
    st.i = 0; st.j = si; st.k = sj;
    q.push(st);
    vector<vector<vector<int>>> dist(M+1, vector<vector<int>>(H, vector<int>(W, INF32)));
    dist[0][si][sj] = 0;
    while(!q.empty()){
        iti now = q.front();
        q.pop();
        rep(l, dx.size()){
            int ni = now.i;
            int nj = now.j + dx[l], nk = now.k + dy[l];
            if(nj<0||H<=nj||nk<0||W<=nk)continue;
            if(G[ni][nj][nk]=='#')continue;
            if(G[ni][nj][nk]>='a'&&G[ni][nj][nk]<'j'&&G[ni][nj][nk]!='a'+ni-1)continue;
            if(dist[ni][nj][nk]!=INF32)continue;
            dist[ni][nj][nk] = dist[now.i][now.j][now.k]+1;
            if(G[ni][nj][nk]>='1'&&G[ni][nj][nk]<='9'){
                ni = G[ni][nj][nk] - '0';
                if(dist[ni][nj][nk]!=INF32)continue;
                dist[ni][nj][nk] = dist[now.i][now.j][now.k]+1;
                iti next;
                next.i = ni; next.j = nj; next.k = nk;
                q.push(next);
            } else {
                iti next;
                next.i = ni; next.j = nj; next.k = nk;
                q.push(next);
            }
        }
    }
    int ans = INF32;
    rep(i, M+1)rep(j, H)rep(k, W)if(G[i][j][k]=='G')ans = min(dist[i][j][k], ans);
    if(ans!=INF32)cout << ans << endl;
    else cout << -1 << endl;
    return 0;
}
0