結果

問題 No.367 ナイトの転身
ユーザー 37kt_37kt_
提出日時 2016-05-16 11:43:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 943 bytes
コンパイル時間 101 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 40,320 KB
最終ジャッジ日時 2024-04-15 22:20:23
合計ジャッジ時間 6,998 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
16,640 KB
testcase_01 AC 34 ms
11,136 KB
testcase_02 AC 33 ms
11,136 KB
testcase_03 AC 33 ms
11,136 KB
testcase_04 AC 32 ms
11,008 KB
testcase_05 AC 32 ms
11,136 KB
testcase_06 AC 36 ms
11,136 KB
testcase_07 AC 33 ms
11,008 KB
testcase_08 AC 33 ms
11,264 KB
testcase_09 AC 37 ms
11,136 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0