結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
はむ吉🐹
|
| 提出日時 | 2016-05-01 20:06:57 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 1,967 ms / 2,000 ms |
| コード長 | 1,778 bytes |
| コンパイル時間 | 119 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 15,616 KB |
| 最終ジャッジ日時 | 2024-10-05 00:51:17 |
| 合計ジャッジ時間 | 6,337 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
#!/usr/bin/env python3
import array
import collections
DS_KNIGHT = [(1, 2), (-1, -2), (1, -2), (-1, 2), (2, 1), (-2, -1), (2, -1), (-2, 1)]
DS_MB = [(1, 1), (-1, -1), (1, -1), (-1, 1)]
INF = 10 ** 8
Status = collections.namedtuple("Status", "is_knight r c")
def min_num_of_operations(height, width, board, start, goal):
s_start = Status(True, start[0], start[1])
dist = [[array.array("L", (INF for _ in range(width))) for _ in range(height)] for _ in range(2)]
dist[s_start.is_knight][s_start.r][s_start.c] = 0
q = collections.deque()
q.append(s_start)
while q:
s0 = q.popleft()
if (s0.r, s0.c) == goal:
break
for dr, dc in (DS_KNIGHT if s0.is_knight else DS_MB):
(r, c) = (s0.r + dr, s0.c + dc)
if r < 0 or r >= height or c < 0 or c >= width:
continue
else:
new_type = s0.is_knight ^ (board[r][c] == 'R')
if dist[int(new_type)][r][c] >= INF:
s = Status(new_type, r, c)
q.append(s)
dist[int(new_type)][r][c] = dist[int(s0.is_knight)][s0.r][s0.c] + 1
return min(dist[True][goal[0]][goal[1]], dist[False][goal[0]][goal[1]])
def main():
height, width = map(int, input().split())
board = []
start = None
goal = None
for r in range(height):
row = input()
if start is None or goal is None:
for c in range(width):
if row[c] == "S":
start = (r, c)
elif row[c] == "G":
goal = (r, c)
board.append(row)
ans = min_num_of_operations(height, width, board, start, goal)
print(ans if ans < INF else -1)
if __name__ == '__main__':
main()
はむ吉🐹