結果

問題 No.3199 Key-Door Grid
コンテスト
ユーザー hirakuuuu
提出日時 2025-07-11 22:20:29
言語 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
結果
AC  
実行時間 74 ms / 3,000 ms
+ 675µs
コード長 1,739 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,194 ms
コンパイル使用メモリ 349,240 KB
実行使用メモリ 21,760 KB
最終ジャッジ日時 2026-07-13 03:50:45
合計ジャッジ時間 5,306 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:53:19: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
   53 |         if(dist[gx][gy][i] == -1) continue;
      |                   ^
main.cpp:23:17: note: 'gx' was declared here
   23 |     int sx, sy, gx, gy;
      |                 ^~
main.cpp:53:23: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
   53 |         if(dist[gx][gy][i] == -1) continue;
      |                       ^
main.cpp:23:21: note: 'gy' was declared here
   23 |     int sx, sy, gx, gy;
      |                     ^~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/uses_allocator_args.h:41,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/memory_resource.h:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/string:72,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bitset:54,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:54,
                 from main.cpp:1:
In constructor 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 0; _Head = int; _Tail = {int, int}]',
    inlined from 'constexpr std::tuple< <template-parameter-1-1> >::tuple(std::tuple< <template-parameter-1-1> >&&) [with _Elements = {int, int, int}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/tuple:996:17,
    inlined from 'constexpr _Tp* std::construct_at(_Tp*, _Args&& ...) [with _Tp = tuple<int, int, int>; _Args = {tuple<int, int, int>}]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@15/15.3.0/include/c++/15/bits/stl_construct.h:110:9,
    inlined from 'static constexpr void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::tuple<int, int, int>; _Args = {std::tuple<in

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define rrep(i, a, n) for(int i = a; i >= n; i--)
#define inr(l, x, r) (l <= x && x < r)
#define ll long long
#define ld long double

// using mint = modint1000000007;
// using mint = modint998244353;
constexpr int IINF = 1001001001;
constexpr ll INF = 1e18;

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}


int main(){
    int h, w, m; cin >> h >> w >> m;
    vector<string> s(h);
    int sx, sy, gx, gy;
    rep(i, 0, h){
        cin >> s[i];
        rep(j, 0, w){
            if(s[i][j] == 'S') sx = i, sy = j;
            if(s[i][j] == 'G') gx = i, gy = j;
        }
    }

    vector<vector<vector<int>>> dist(h, vector<vector<int>>(w, vector<int>(m+1, -1)));
    queue<tuple<int, int, int>> que;
    dist[sx][sy][m] = 0;
    que.push({sx, sy, m});
    while(!que.empty()){
        auto [px, py, k] = que.front(); que.pop();
        rep(i, 0, 4){
            int nx = px+(i-1)%2, ny = py+(i-2)%2;
            int nk = k;
            if(!inr(0, nx, h) || !inr(0, ny, w)) continue;
            if(s[nx][ny] == '#') continue;
            if(inr(0, s[nx][ny]-'a', m) && (s[nx][ny]-'a') != k) continue;
            if(inr(0, s[nx][ny]-'1', m)) nk = s[nx][ny]-'1';
            if(dist[nx][ny][nk] != -1) continue;
            dist[nx][ny][nk] = dist[px][py][k]+1;
            que.push({nx, ny, nk});
        }
    }

    int ans = IINF;
    rep(i, 0, m+1){
        if(dist[gx][gy][i] == -1) continue;
        chmin(ans, dist[gx][gy][i]);
    }
    if(ans == IINF) ans = -1;
    cout << ans << endl;

    
    return 0;
}
0