結果
問題 | No.367 ナイトの転身 |
ユーザー |
![]() |
提出日時 | 2016-05-16 11:43:07 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 943 bytes |
コンパイル時間 | 108 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 40,064 KB |
最終ジャッジ日時 | 2024-10-06 04:21:05 |
合計ジャッジ時間 | 6,830 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 TLE * 2 -- * 15 |
ソースコード
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)