結果

問題 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
コンパイル時間 221 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 18,084 KB
最終ジャッジ日時 2024-05-08 03:11:23
合計ジャッジ時間 4,662 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
18,084 KB
testcase_01 AC 32 ms
11,008 KB
testcase_02 AC 33 ms
10,880 KB
testcase_03 AC 32 ms
10,880 KB
testcase_04 AC 31 ms
11,008 KB
testcase_05 AC 31 ms
10,880 KB
testcase_06 AC 34 ms
11,008 KB
testcase_07 AC 33 ms
11,008 KB
testcase_08 AC 33 ms
10,880 KB
testcase_09 AC 32 ms
11,008 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