結果

問題 No.3199 Key-Door Grid
ユーザー tkykwtnb
提出日時 2025-07-11 22:27:34
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 711 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 81,352 KB
実行使用メモリ 345,172 KB
最終ジャッジ日時 2025-07-11 22:27:40
合計ジャッジ時間 5,211 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other TLE * 1 -- * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
H,W,M=map(int,input().split())
S=[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=set()
Q=deque([f"{s[0]} {s[1]} {0} {0}"])
while Q:
    h,w,cnt,key=map(int,(Q.popleft()).split())
    vis.add((h,w))
    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 (h+dh,w+dw) in vis:continue
        if "a"<=S[h+dh][w+dw]<="i" and ord(S[h+dh][w+dw])-ord("a")+1!=key:continue
        Q.append(f"{h+dh} {w+dw} {cnt+1} {key}")
print(-1)
0