結果

問題 No.3199 Key-Door Grid
ユーザー t98slider
提出日時 2025-07-11 21:38:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 124 ms / 3,000 ms
コード長 1,392 bytes
コンパイル時間 1,929 ms
コンパイル使用メモリ 213,944 KB
実行使用メモリ 21,504 KB
最終ジャッジ日時 2025-07-11 21:38:45
合計ジャッジ時間 4,873 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w, m;
    cin >> h >> w >> m;
    vector<string> A(h);
    for(auto &&str : A) cin >> str;

    vector dp(h, vector(w, vector<int>(10, 1 << 30)));
    dp.emplace_back(vector(1, vector<int>(10, 1 << 30)));
    queue<tuple<int,int,int>> que;
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            if(A[y][x] == 'S'){
                dp[y][x][0] = 0;
                que.emplace(y, x, 0);
            }
        }
    }
    vector<pair<int,int>> dir = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
    while(!que.empty()){
        auto [y, x, s] = que.front();
        que.pop();
        if(A[y][x] == 'G'){
            cout << dp[y][x][s] << '\n';
            return 0;
        }
        for(auto [ny, nx] : dir){
            ny += y, nx += x;
            if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
            int ns = isdigit(A[y][x]) ? A[y][x] - '0' : s;
            if(A[ny][nx] == '#') continue;
            if('a' <= A[y][x] && A[y][x] <= 'z'){
                int ss = (A[y][x] - 'a') + 1;
                if(ns != ss) continue;
            }
            if(dp[y][x][s] + 1 >= dp[ny][nx][ns]) continue;
            dp[ny][nx][ns] = dp[y][x][s] + 1;
            que.emplace(ny, nx, ns);
        }
    }
    cout << "-1\n";
}
0