結果

問題 No.3199 Key-Door Grid
ユーザー 回転
提出日時 2025-07-11 21:43:47
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,098 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 445,028 KB
最終ジャッジ日時 2025-07-11 21:43:54
合計ジャッジ時間 4,928 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other TLE * 1 -- * 36
権限があれば一括ダウンロードができます

ソースコード

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 = set()
q = deque([(0,sy,sx,-1)])
while(q):
    v,y,x,key = q.popleft()

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

    if((y,x,key) in visited):continue
    visited.add((y,x,key))

    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.appendleft((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)
0