結果

問題 No.3199 Key-Door Grid
コンテスト
ユーザー tkykwtnb
提出日時 2025-07-11 22:17:07
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
MLE  
実行時間 -
コード長 727 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 233 ms
コンパイル使用メモリ 96,108 KB
実行使用メモリ 1,094,776 KB
最終ジャッジ日時 2026-07-13 03:48:36
合計ジャッジ時間 6,336 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other MLE * 1 -- * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque,defaultdict
H,W,M=map(int,input().split())
S=[list(input()) for _ in range(H)]
for h in range(H):
    for w in range(W):
        if S[h][w]=="S":s=(h,w)
        elif S[h][w]=="G":g=(h,w)
vis=[[0 for _ in range(W)] for _ in range(H)]
Q=deque([(s,0,0)])
while Q:
    (h,w),cnt,key=Q.popleft()
    vis[h][w]=1
    if (h,w)==g:
        exit(print(cnt))
    if "1"<=S[h][w]<="9":key=int(S[h][w])
    for dh,dw in ((-1,0),(1,0),(0,-1),(0,1)):
        if not (0<=h+dh<H and 0<=w+dw<W):continue
        if S[h+dh][w+dw]=="#":continue
        if vis[h+dh][w+dw]:continue
        if "a"<=S[h+dh][w+dw]<="i" and ord(S[h+dh][w+dw])-ord("a")+1!=key:continue
        Q.append(((h+dh,w+dw),cnt+1,key))
print(-1)
0