結果

問題 No.367 ナイトの転身
ユーザー maspymaspy
提出日時 2020-03-19 13:29:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 417 ms / 2,000 ms
コード長 1,427 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 145,532 KB
最終ジャッジ日時 2023-08-20 20:47:17
合計ジャッジ時間 5,911 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
71,416 KB
testcase_01 AC 97 ms
71,496 KB
testcase_02 AC 101 ms
71,620 KB
testcase_03 AC 97 ms
71,540 KB
testcase_04 AC 98 ms
71,628 KB
testcase_05 AC 98 ms
71,636 KB
testcase_06 AC 111 ms
77,040 KB
testcase_07 AC 99 ms
71,616 KB
testcase_08 AC 99 ms
71,848 KB
testcase_09 AC 97 ms
71,616 KB
testcase_10 AC 363 ms
145,404 KB
testcase_11 AC 417 ms
145,532 KB
testcase_12 AC 273 ms
101,404 KB
testcase_13 AC 240 ms
101,704 KB
testcase_14 AC 242 ms
100,992 KB
testcase_15 AC 139 ms
77,832 KB
testcase_16 AC 235 ms
99,920 KB
testcase_17 AC 146 ms
78,412 KB
testcase_18 AC 159 ms
81,616 KB
testcase_19 AC 171 ms
84,608 KB
testcase_20 AC 146 ms
78,712 KB
testcase_21 AC 176 ms
84,572 KB
testcase_22 AC 112 ms
77,384 KB
testcase_23 AC 132 ms
77,584 KB
testcase_24 AC 130 ms
77,784 KB
testcase_25 AC 129 ms
77,780 KB
testcase_26 AC 123 ms
77,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import itertools
from collections import deque

H, W = map(int, readline().split())
S = ''.join(read().decode().split())

start = S.index('S')
goal = S.index('G')
N = H * W

Knight = ((2, 1), (1, 2), (-1, 2), (-2, 1), (-2, -1), (-1, -2), (1, -2), (2, -1))
Bishop = ((1, 1), (-1, 1), (-1, -1), (1, -1))

# 0 <= i < N:knightで出発
# N <= i < 2N:bishopで出発

graph = [[] for _ in range(N + N)]
for x, y in itertools.product(range(H), range(W)):
    for dx, dy in Knight:
        x1 = x + dx
        y1 = y + dy
        if not ((0 <= x1 < H) and (0 <= y1 < W)):
            continue
        i = x * W + y
        j = x1 * W + y1
        if S[j] == 'R':
            j += N
        graph[i].append(j)
    for dx, dy in Bishop:
        x1 = x + dx
        y1 = y + dy
        if not ((0 <= x1 < H) and (0 <= y1 < W)):
            continue
        i = x * W + y + N
        j = x1 * W + y1
        if S[j] != 'R':
            j += N
        graph[i].append(j)


INF = 10 ** 6
dist = [INF] * (N + N)
dist[start] = 0
q = deque([start])

while q:
    v = q.popleft()
    dw = dist[v] + 1
    for w in graph[v]:
        if dist[w] <= dw:
            continue
        dist[w] = dw
        q.append(w)

answer = min(dist[goal], dist[goal + N])
if answer == INF:
    answer = -1
print(answer)
0