結果

問題 No.367 ナイトの転身
ユーザー 👑 rin204rin204
提出日時 2022-11-03 01:03:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 1,140 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 86,656 KB
最終ジャッジ日時 2023-09-24 11:43:28
合計ジャッジ時間 5,677 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,452 KB
testcase_01 AC 98 ms
71,392 KB
testcase_02 AC 93 ms
71,708 KB
testcase_03 AC 95 ms
71,520 KB
testcase_04 AC 95 ms
71,700 KB
testcase_05 AC 92 ms
71,520 KB
testcase_06 AC 101 ms
72,212 KB
testcase_07 AC 96 ms
71,760 KB
testcase_08 AC 92 ms
71,468 KB
testcase_09 AC 94 ms
71,700 KB
testcase_10 AC 193 ms
82,120 KB
testcase_11 AC 253 ms
86,656 KB
testcase_12 AC 211 ms
80,976 KB
testcase_13 AC 193 ms
81,196 KB
testcase_14 AC 203 ms
80,640 KB
testcase_15 AC 143 ms
78,168 KB
testcase_16 AC 210 ms
80,612 KB
testcase_17 AC 151 ms
77,844 KB
testcase_18 AC 155 ms
78,180 KB
testcase_19 AC 164 ms
78,440 KB
testcase_20 AC 148 ms
78,216 KB
testcase_21 AC 170 ms
78,476 KB
testcase_22 AC 108 ms
76,884 KB
testcase_23 AC 130 ms
77,600 KB
testcase_24 AC 137 ms
78,060 KB
testcase_25 AC 129 ms
77,384 KB
testcase_26 AC 119 ms
77,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

h, w = map(int, input().split())
S = [input() for _ in range(h)]
inf = 1 << 60
dist = [inf] * (h * w * 2)

def f(i, j, t):
    return (i * w + j) * 2 + t

directions = []
directions.append([])
for i in [-2, -1, 1, 2]:
    for j in [-2, -1, 1, 2]:
        if abs(i) != abs(j):
            directions[0].append((i, j))
directions.append(((1, 1), (1, -1), (-1, 1), (-1, -1)))

for i in range(h):
    for j in range(w):
        if S[i][j] == "S":
            si = i
            sj = j
        elif S[i][j] == "G":
            gi = i
            gj = j

queue = deque()
dist[f(si, sj, 0)] = 0
queue.append((si, sj, 0))
while queue:
    i, j, t = queue.popleft()
    for di, dj in directions[t]:
        ni = i + di
        nj = j + dj
        if ni < 0 or ni >= h or nj < 0 or nj >= w:
            continue
        if S[ni][nj] == "R":
            nt = t ^ 1
        else:
            nt = t
        if dist[f(ni, nj, nt)] == inf:
            dist[f(ni, nj, nt)] = dist[f(i, j, t)] + 1
            queue.append((ni, nj, nt))

ans = min(dist[f(gi, gj, 0)], dist[f(gi, gj, 1)])
if ans == inf:
    ans = -1
print(ans)
0