結果

問題 No.3199 Key-Door Grid
ユーザー tkykwtnb
提出日時 2025-07-18 20:54:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,004 bytes
コンパイル時間 479 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 86,768 KB
最終ジャッジ日時 2025-07-18 20:54:41
合計ジャッジ時間 8,417 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13 WA * 23 RE * 1
権限があれば一括ダウンロードができます

ソースコード

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)
def pack(h,w,cnt,key,k):
    return h*(k**3)+w*(k**2)+cnt*k+key
def unpack(v,k):
    return v//(k**3),v%(k**3)//(k**2),v%(k**2)//k,v%k
K=500
D={"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9}
vis=[[K*K for _ in range(W)] for _ in range(H)]
Q=deque([pack(s[0],s[1],0,0,K)])
while Q:
    h,w,cnt,key=unpack(Q.popleft(),K)
    if cnt>=vis[h][w]:continue
    vis[h][w]=cnt
    #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 cnt>=vis[h+dh][w+dw]:continue
        if "a"<=S[h+dh][w+dw]<="i" and D[S[h+dh][w+dw]]!=key:continue
        Q.append(pack(h+dh,w+dw,cnt+1,key,K))
if vis[g[0]][g[1]]<K*K:print(vis[g[0]][g[1]])
else:print(-1)
0