結果

問題 No.367 ナイトの転身
ユーザー nebukuro09nebukuro09
提出日時 2016-10-06 10:23:01
言語 Python2
(2.7.18)
結果
TLE  
実行時間 -
コード長 984 bytes
コンパイル時間 49 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 74,468 KB
最終ジャッジ日時 2024-05-01 13:29:19
合計ジャッジ時間 6,376 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
13,756 KB
testcase_01 AC 11 ms
6,940 KB
testcase_02 AC 11 ms
6,944 KB
testcase_03 AC 12 ms
6,940 KB
testcase_04 AC 12 ms
6,940 KB
testcase_05 AC 11 ms
6,940 KB
testcase_06 AC 11 ms
6,940 KB
testcase_07 AC 10 ms
6,940 KB
testcase_08 AC 11 ms
6,940 KB
testcase_09 AC 11 ms
6,944 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 collections import deque

H, W = map(int, raw_input().split())
board = [raw_input() for _ in xrange(H)]
for i in xrange(H):
    if 'S' in board[i]:
        start = (i, board[i].index('S'))
    if 'G' in board[i]:
        goal = (i, board[i].index('G'))
move = {'K':[(-2, -1), (-2, 1), (2, -1), (2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2)],
        'B':[(-1, -1), (-1, 1), (1, -1), (1, 1)]}
tsugi = {'K':'B', 'B':'K'}

q = deque()
q.append((start[0], start[1], 'K', 0))
visited = set()
while len(q) > 0:
    r, c, koma, depth = q.popleft()
    if (r, c, koma) in visited:
        continue
    if (r, c) == goal:
        print depth
        exit()
    visited.add((r, c, koma))
    for dr, dc in move[koma]:
        nr, nc = r+dr, c+dc
        if nr < 0 or nr >= H or nc < 0 or nc >= W:
            continue
        nkoma = tsugi[koma] if board[nr][nc] == 'R' else koma
        if (nr, nc, nkoma) in visited:
            continue
        q.append((nr, nc, nkoma, depth+1))
print -1
0