結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2024-08-21 00:02:16 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,800 ms / 2,000 ms |
| コード長 | 1,285 bytes |
| コンパイル時間 | 450 ms |
| コンパイル使用メモリ | 82,252 KB |
| 実行使用メモリ | 273,668 KB |
| 最終ジャッジ日時 | 2024-08-21 00:02:23 |
| 合計ジャッジ時間 | 7,370 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
from collections import deque
H, W = map(int, input().split())
G = [input() for _ in range(H)]
def find(c) -> tuple[int, int]:
for i in range(H):
for j in range(W):
if G[i][j] == c:
return i, j
assert False
def move_knight(r, c):
res = []
for dr, dc in [(-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1)]:
nr = r + dr
nc = c + dc
if not (0 <= nr < H and 0 <= nc < W): continue
res.append((nr, nc))
return res
def move_bishop(r, c):
res = []
for dr, dc in [(-1, 1), (1, 1), (1, -1), (-1, -1)]:
nr = r + dr
nc = c + dc
if not (0 <= nr < H and 0 <= nc < W): continue
res.append((nr, nc))
return res
KNIGHT = 0
BISHOP = 1
start = find('S')
q = deque([(start[0], start[1], 0, 0)])
used = set()
while q:
r, c, step, piece = q.popleft()
if (r, c, piece) in used: continue
used.add((r, c, piece))
if G[r][c] == 'G':
print(step)
break
movefn = move_knight if piece == 0 else move_bishop
for nr, nc in movefn(r, c):
np = piece
if G[nr][nc] == 'R':
np ^= 1
if (nr, nc, np) in used: continue
q.append((nr, nc, step+1, np))
else:
print(-1)
norioc