結果

問題 No.367 ナイトの転身
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-05 20:30:53
言語 Python2
(2.7.18)
結果
TLE  
実行時間 -
コード長 1,519 bytes
コンパイル時間 444 ms
コンパイル使用メモリ 7,076 KB
実行使用メモリ 44,960 KB
最終ジャッジ日時 2024-04-14 22:10:33
合計ジャッジ時間 7,063 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
13,856 KB
testcase_01 AC 14 ms
6,944 KB
testcase_02 AC 14 ms
7,040 KB
testcase_03 AC 15 ms
7,040 KB
testcase_04 AC 15 ms
7,040 KB
testcase_05 AC 15 ms
7,040 KB
testcase_06 AC 15 ms
7,168 KB
testcase_07 AC 14 ms
7,040 KB
testcase_08 AC 15 ms
6,940 KB
testcase_09 AC 14 ms
7,040 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 #

import Queue

H, W = map(int, raw_input().split())
field = [raw_input() for i in range(H)]

sx, sy, gx, gy = -1, -1, -1, -1
for i in range(H):
    for j in range(W):
        if field[i][j] == "S":
            sx = i
            sy = j
        if field[i][j] == "G":
            gx = i
            gy = j

inf = 99999999

d = [[[inf] * 2 for j in range(W)] for i in range(H)]
d[sx][sy][1] = 0
que = Queue.Queue()
que.put((sx, sy, 1))    # x, y, mode(1 = knight, 0 = mini-bishop)

while not que.empty():
    x, y, mode = que.get()
    if mode == 1:
        # knight
        dx = [2, 2, 1, 1, -1, -1, -2, -2];
        dy = [1, -1, 2, -2, 2, -2, 1, -1];
        for k in range(8):
            nx = x + dx[k]
            ny = y + dy[k]
            if not (0 <= nx < H and 0 <= ny < W):
                continue
            nxt_mode = mode ^ (field[nx][ny] == "R")
            if d[nx][ny][nxt_mode] == inf:
                d[nx][ny][nxt_mode] = d[x][y][mode] + 1
                que.put((nx, ny, nxt_mode))
    else:
        # mini-bishop
        dx = [1, 1, -1, -1]
        dy = [1, -1, 1, -1]
        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]
            if not (0 <= nx < H and 0 <= ny < W):
                continue
            nxt_mode = mode ^ (field[nx][ny] == "R")
            if d[nx][ny][nxt_mode] == inf:
                d[nx][ny][nxt_mode] = d[x][y][mode] + 1
                que.put((nx, ny, nxt_mode))

ans = min(d[gx][gy][0], d[gx][gy][1])
if ans == inf:
    ans = -1
print ans
0