結果
| 問題 |
No.3199 Key-Door Grid
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-07-12 20:41:48 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 968 bytes |
| コンパイル時間 | 310 ms |
| コンパイル使用メモリ | 82,144 KB |
| 実行使用メモリ | 94,848 KB |
| 最終ジャッジ日時 | 2025-07-12 20:42:33 |
| 合計ジャッジ時間 | 44,218 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 13 WA * 23 RE * 1 |
ソースコード
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 "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 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)