結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2024-08-21 00:05:20 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 658 ms / 2,000 ms |
| コード長 | 1,522 bytes |
| コンパイル時間 | 349 ms |
| コンパイル使用メモリ | 82,568 KB |
| 実行使用メモリ | 157,704 KB |
| 最終ジャッジ日時 | 2024-08-21 00:05:25 |
| 合計ジャッジ時間 | 4,941 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
from collections import deque
def list3(a, b, c, *, val=0):
return [[[val] * c for _ in range(b)] for _ in range(a)]
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()
used = list3(H, W, 2, val=False)
while q:
r, c, step, piece = q.popleft()
# if (r, c, piece) in used: continue
# used.add((r, c, piece))
if used[r][c][piece]: continue
used[r][c][piece] = True
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
if used[nr][nc][np]: continue
q.append((nr, nc, step+1, np))
else:
print(-1)
norioc