結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
maspy
|
| 提出日時 | 2020-03-19 13:29:35 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,427 bytes |
| コンパイル時間 | 117 ms |
| コンパイル使用メモリ | 12,928 KB |
| 実行使用メモリ | 172,976 KB |
| 最終ジャッジ日時 | 2024-12-14 03:08:26 |
| 合計ジャッジ時間 | 14,867 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 TLE * 2 |
ソースコード
#!/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)
maspy