結果

問題 No.367 ナイトの転身
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-02 02:21:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 338 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 101,760 KB
最終ジャッジ日時 2024-04-19 20:07:53
合計ジャッジ時間 4,578 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,760 KB
testcase_01 AC 51 ms
53,760 KB
testcase_02 AC 44 ms
53,760 KB
testcase_03 AC 47 ms
53,760 KB
testcase_04 AC 44 ms
54,016 KB
testcase_05 AC 51 ms
53,760 KB
testcase_06 AC 47 ms
54,528 KB
testcase_07 AC 43 ms
53,632 KB
testcase_08 AC 43 ms
53,760 KB
testcase_09 AC 52 ms
53,632 KB
testcase_10 AC 245 ms
100,224 KB
testcase_11 AC 338 ms
101,760 KB
testcase_12 AC 203 ms
86,144 KB
testcase_13 AC 176 ms
86,016 KB
testcase_14 AC 193 ms
85,760 KB
testcase_15 AC 93 ms
72,832 KB
testcase_16 AC 173 ms
84,736 KB
testcase_17 AC 111 ms
77,440 KB
testcase_18 AC 119 ms
77,824 KB
testcase_19 AC 118 ms
78,336 KB
testcase_20 AC 115 ms
77,824 KB
testcase_21 AC 124 ms
78,464 KB
testcase_22 AC 65 ms
62,080 KB
testcase_23 AC 77 ms
67,584 KB
testcase_24 AC 80 ms
68,224 KB
testcase_25 AC 72 ms
66,432 KB
testcase_26 AC 70 ms
64,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

move = ((
    (1, 2), (1, -2), (-1, 2), (-1, -2),
    (2, 1), (2, -1), (-2, 1), (-2, -1)
), (
    (-1, -1), (1, 1), (1, -1), (-1, 1)
))

H, W = map(int, input().split())
G = [input().rstrip() for _ in range(H)]
for i in range(H):
    for j in range(W):
        if G[i][j] == "S":
            sx, sy = i, j
        if G[i][j] == "G":
            gx, gy = i, j

inf = 10**9
dist = [[[inf] * 2 for _ in range(W)] for _ in range(H)]
dist[sx][sy][0] = 0
que = deque([(sx, sy, 0)])
while que:
    x, y, f = que.popleft()
    d = dist[x][y][f]
    for dx, dy in move[f]:
        nx = x + dx
        ny = y + dy
        if 0 <= nx < H and 0 <= ny < W:
            nf = f
            if G[nx][ny] == "R":
                nf = f ^ 1
            if dist[nx][ny][nf] > d + 1:
                dist[nx][ny][nf] = d + 1
                que.append((nx, ny, nf))

ans = min(dist[gx][gy][0], dist[gx][gy][1])
if ans >= inf:
    ans = -1
print(ans)
0