結果

問題 No.3199 Key-Door Grid
ユーザー 回転
提出日時 2025-07-11 21:54:21
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 259 ms / 3,000 ms
コード長 2,740 bytes
コンパイル時間 3,677 ms
コンパイル使用メモリ 293,512 KB
実行使用メモリ 33,792 KB
最終ジャッジ日時 2025-07-11 21:54:33
合計ジャッジ時間 8,051 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
from collections import deque
H,W,M = list(map(int,input().split()))
S = [input() for _ in range(H)]

for i in range(H):
    for j in range(W):
        if(S[i][j] == "S"):
            sy,sx = i,j
        elif(S[i][j] == "G"):
            gy,gx = i,j

dy = [-1,1,0,0]
dx = [0,0,-1,1]
visited = [[[10**18 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]
q = deque([(0,sy,sx,M)])
while(q):
    v,y,x,key = q.popleft()

    if(y == gy and x == gx):
        print(v)
        exit()

    if(visited[y][x][key] <= v):continue
    visited[y][x][key] = v

    for i in range(4):
        next_y = y + dy[i]
        next_x = x + dx[i]
        if(next_y < 0 or H <= next_y):continue
        if(next_x < 0 or W <= next_x):continue
        if(S[next_y][next_x] == "#"):continue

        if(S[next_y][next_x].isnumeric()):
            q.append((v+1,next_y,next_x,int(S[next_y][next_x])-1))
        elif(S[next_y][next_x] == "." or S[next_y][next_x] == "S" or S[next_y][next_x] == "G"):
            q.append((v+1,next_y,next_x,key))
        else:
            if(key == ord(S[next_y][next_x]) - ord("a")):
                q.append((v+1,next_y,next_x,key))

print(-1)
*/

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

int main() {
    int H, W, M;
    cin >> H >> W >> M;
    vector<string> S(H);
    for (int i = 0; i < H; ++i) cin >> S[i];

    int sy = -1, sx = -1, gy = -1, gx = -1;
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            if (S[i][j] == 'S') sy = i, sx = j;
            if (S[i][j] == 'G') gy = i, gx = j;
        }
    }

    const int dy[4] = {-1, 1, 0, 0};
    const int dx[4] = {0, 0, -1, 1};

    vector<vector<vector<ll>>> visited(H, vector<vector<ll>>(W, vector<ll>(M + 1, 1e18)));
    deque<tuple<ll, int, int, int>> q;
    q.emplace_back(0, sy, sx, M);

    while (!q.empty()) {
        auto [v, y, x, key] = q.front(); q.pop_front();

        if (y == gy && x == gx) {
            cout << v << endl;
            return 0;
        }

        if (visited[y][x][key] <= v) continue;
        visited[y][x][key] = v;

        for (int i = 0; i < 4; ++i) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
            char ch = S[ny][nx];
            if (ch == '#') continue;

            if (isdigit(ch)) {
                q.emplace_back(v + 1, ny, nx, ch - '0' - 1);
            } else if (ch == '.' || ch == 'S' || ch == 'G') {
                q.emplace_back(v + 1, ny, nx, key);
            } else {
                if (key == ch - 'a') {
                    q.emplace_back(v + 1, ny, nx, key);
                }
            }
        }
    }

    cout << -1 << endl;
    return 0;
}
0