結果

問題 No.3199 Key-Door Grid
ユーザー srjywrdnprkt
提出日時 2025-07-25 14:39:04
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 124 ms / 3,000 ms
コード長 2,149 bytes
コンパイル時間 3,640 ms
コンパイル使用メモリ 293,296 KB
実行使用メモリ 21,632 KB
最終ジャッジ日時 2025-07-25 14:39:13
合計ジャッジ時間 8,084 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:67:52: warning: ‘gh’ may be used uninitialized [-Wmaybe-uninitialized]
   67 |     for (int i=0; i<=M; i++) ans = min(ans, dist[gh][gw][i]);
      |                                                    ^
main.cpp:23:9: note: ‘gh’ was declared here
   23 |     int gh, gw;
      |         ^~
main.cpp:67:56: warning: ‘gw’ may be used uninitialized [-Wmaybe-uninitialized]
   67 |     for (int i=0; i<=M; i++) ans = min(ans, dist[gh][gw][i]);
      |                                                        ^
main.cpp:23:13: note: ‘gw’ was declared here
   23 |     int gh, gw;
      |             ^~

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/modint>

using namespace std;
//using namespace atcoder;
using ll = long long;
//using mint = modint998244353;

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    /*
       (i,j,k) = 持っている鍵がkの状態で(i,j)に行くことができるか?
    */

    int H, W, M;
    cin >> H >> W >> M;
    vector dist(H, vector(W, vector<int>(M+1, 1e9)));
    queue<tuple<int, int, int>> que;
    vector<string> S(H);
    for (int i=0; i<H; i++) cin >> S[i];
    int gh, gw;
    for (int i=0; i<H; i++){
        for (int j=0; j<W; j++){
            if (S[i][j] == 'S'){
                que.push({i, j, 0});
                dist[i][j][0] = 0;
            }
            else if (S[i][j] == 'G'){
                gh = i; gw = j;
            }
        }
    }
    int dx[4] = {1,0,-1,0};
    int dy[4] = {0,1,0,-1};
    auto check=[&](int i, int j){return i != -1 && i != H && j != -1 && j != W;};
    while(!que.empty()){
        auto [h,w,k] = que.front();
        que.pop();
        for (int i=0; i<4; i++){
            int nh=h+dx[i], nw=w+dy[i], nk;
            if (check(nh, nw) && S[nh][nw] != '#'){
                if ('1' <= S[nh][nw] && S[nh][nw] <= '9'){
                    nk = S[nh][nw] - '0';
                    if (dist[nh][nw][nk] == 1e9){
                        dist[nh][nw][nk] = dist[h][w][k]+1;
                        que.push({nh, nw, nk});
                    }
                }
                else if ('a' <= S[nh][nw] && S[nh][nw] <= 'z'){
                    if (k == S[nh][nw]-'a'+1 && dist[nh][nw][k] == 1e9){
                        dist[nh][nw][k] = dist[h][w][k]+1;
                        que.push({nh, nw, k});
                    }
                }
                else{
                    if (dist[nh][nw][k] == 1e9){
                        dist[nh][nw][k] = dist[h][w][k]+1;
                        que.push({nh, nw, k});
                    }
                }
            }
        }
    }
    int ans = 1e9;
    for (int i=0; i<=M; i++) ans = min(ans, dist[gh][gw][i]);
    cout << (ans == 1e9 ? -1 : ans) << endl;

    return 0;
}
0