結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
37kt_
|
| 提出日時 | 2016-05-16 11:43:49 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 635 ms / 2,000 ms |
| コード長 | 943 bytes |
| コンパイル時間 | 586 ms |
| コンパイル使用メモリ | 81,928 KB |
| 実行使用メモリ | 105,820 KB |
| 最終ジャッジ日時 | 2024-12-23 13:26:48 |
| 合計ジャッジ時間 | 6,135 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
from queue import Queue
dx = [[-2, -2, -1, 1, 2, 2, 1, -1], [-1, -1, 1, 1]]
dy = [[-1, 1, 2, 2, 1, -1, -2, -2], [-1, 1, 1, -1]]
h, w = map(int, input().split())
s = [input() for i in range(h)]
sx, sy, gx, gy = [-1, -1, -1, -1]
for i, line in enumerate(s):
for j, c in enumerate(line):
if c == "S":
sx, sy = i, j
elif c == "G":
gx, gy = i, j
dist = [[ [-1, -1] for j in range(w)] for i in range(h)]
q = Queue()
dist[sx][sy][0] = 0
q.put((sx, sy, 0))
while not q.empty():
x, y, d = q.get()
for i in range(len(dx[d])):
nx, ny = x + dx[d][i], y + dy[d][i]
if nx < 0 or h <= nx or ny < 0 or w <= ny:
continue
nd = d ^ (1 if s[nx][ny] == "R" else 0)
if dist[nx][ny][nd] == -1:
dist[nx][ny][nd] = dist[x][y][d] + 1
q.put((nx, ny, nd))
res = -1
if dist[gx][gy][0] == -1:
res = dist[gx][gy][1]
elif dist[gx][gy][1] == -1:
res = dist[gx][gy][0]
else:
res = min(dist[gx][gy])
print(res)
37kt_