結果

問題 No.367 ナイトの転身
ユーザー ntuda
提出日時 2025-06-23 22:14:04
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,090 bytes
コンパイル時間 544 ms
コンパイル使用メモリ 82,224 KB
実行使用メモリ 125,196 KB
最終ジャッジ日時 2025-06-23 22:14:09
合計ジャッジ時間 4,681 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

H, W = map(int, input().split())
S = [input() for _ in range(H)]
RC = set()
dir = [[(1, 2), (2, 1), (1, -2), (2, -1), (-1, 2), (-2, 1), (-1, -2), (-2, -1)],
       [(1, 1), (1, -1), (1, 1), (1, -1)]]

for i in range(H):
    for j in range(W):
        if S[i][j] == "S":
            ST = (i, j, 0)
        elif S[i][j] == "G":
            GL = (i, j)
        elif S[i][j] == "R":
            RC.add((i, j))
D = [[[-1] * W for _ in range(H)] for _ in range(2)]
D[0][ST[0]][ST[1]] = 0
Q = set([ST])
Q2 = set()
cnt = 1
while Q:
    while Q:
        x, y, z = Q.pop()
        for dx, dy in dir[z]:
            x2 = x + dx
            y2 = y + dy
            if (x2, y2) == GL:
                print(cnt)
                exit()
            elif (x2, y2) in RC:
                if D[z ^ 1][x2][y2] == -1:
                    D[z ^ 1][x2][y2] = cnt
                    Q2.add((x2, y2, z ^ 1))
            elif 0 <= x2 < H and 0 <= y2 < W:
                if D[z][x2][y2] == -1:
                    D[z][x2][y2] = cnt
                    Q2.add((x2, y2, z))
    cnt += 1
    Q2, Q = Q, Q2
print(-1)
0