結果

問題 No.367 ナイトの転身
ユーザー maspymaspy
提出日時 2020-03-19 13:29:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,427 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 10,908 KB
実行使用メモリ 170,108 KB
最終ジャッジ日時 2023-08-20 20:47:10
合計ジャッジ時間 4,532 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
15,712 KB
testcase_01 AC 20 ms
8,632 KB
testcase_02 AC 19 ms
8,612 KB
testcase_03 AC 19 ms
8,676 KB
testcase_04 AC 20 ms
8,736 KB
testcase_05 AC 19 ms
8,712 KB
testcase_06 AC 22 ms
8,692 KB
testcase_07 AC 20 ms
8,608 KB
testcase_08 AC 19 ms
8,660 KB
testcase_09 AC 20 ms
8,724 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

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