結果

問題 No.367 ナイトの転身
ユーザー nebukuro09nebukuro09
提出日時 2016-10-06 10:23:16
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 1,262 ms / 2,000 ms
コード長 984 bytes
コンパイル時間 1,633 ms
コンパイル使用メモリ 76,300 KB
実行使用メモリ 244,184 KB
最終ジャッジ日時 2024-05-01 13:29:27
合計ジャッジ時間 7,387 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
77,324 KB
testcase_01 AC 84 ms
77,312 KB
testcase_02 AC 84 ms
77,568 KB
testcase_03 AC 84 ms
77,696 KB
testcase_04 AC 82 ms
77,576 KB
testcase_05 AC 84 ms
77,340 KB
testcase_06 AC 84 ms
77,340 KB
testcase_07 AC 83 ms
77,592 KB
testcase_08 AC 82 ms
77,456 KB
testcase_09 AC 83 ms
77,592 KB
testcase_10 AC 595 ms
149,448 KB
testcase_11 AC 1,262 ms
244,184 KB
testcase_12 AC 185 ms
84,940 KB
testcase_13 AC 172 ms
85,560 KB
testcase_14 AC 328 ms
104,796 KB
testcase_15 AC 106 ms
79,232 KB
testcase_16 AC 321 ms
104,800 KB
testcase_17 AC 145 ms
81,252 KB
testcase_18 AC 168 ms
84,108 KB
testcase_19 AC 148 ms
82,304 KB
testcase_20 AC 109 ms
79,404 KB
testcase_21 AC 148 ms
82,452 KB
testcase_22 AC 92 ms
78,592 KB
testcase_23 AC 102 ms
79,292 KB
testcase_24 AC 105 ms
79,008 KB
testcase_25 AC 108 ms
79,104 KB
testcase_26 AC 102 ms
79,488 KB
権限があれば一括ダウンロードができます

ソースコード

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