結果

問題 No.367 ナイトの転身
ユーザー rpy3cpprpy3cpp
提出日時 2016-05-28 10:11:20
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 966 ms / 2,000 ms
コード長 1,800 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 14,780 KB
最終ジャッジ日時 2023-07-29 11:40:29
合計ジャッジ時間 4,772 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,080 KB
testcase_01 AC 17 ms
8,140 KB
testcase_02 AC 16 ms
8,148 KB
testcase_03 AC 17 ms
8,020 KB
testcase_04 AC 16 ms
7,996 KB
testcase_05 AC 16 ms
8,204 KB
testcase_06 AC 18 ms
8,000 KB
testcase_07 AC 17 ms
7,960 KB
testcase_08 AC 17 ms
7,988 KB
testcase_09 AC 17 ms
8,092 KB
testcase_10 AC 555 ms
14,564 KB
testcase_11 AC 966 ms
14,780 KB
testcase_12 AC 74 ms
10,908 KB
testcase_13 AC 94 ms
10,628 KB
testcase_14 AC 229 ms
10,652 KB
testcase_15 AC 22 ms
8,496 KB
testcase_16 AC 227 ms
10,252 KB
testcase_17 AC 46 ms
8,444 KB
testcase_18 AC 61 ms
8,460 KB
testcase_19 AC 55 ms
9,088 KB
testcase_20 AC 24 ms
8,600 KB
testcase_21 AC 59 ms
8,828 KB
testcase_22 AC 17 ms
7,996 KB
testcase_23 AC 17 ms
8,204 KB
testcase_24 AC 21 ms
7,996 KB
testcase_25 AC 20 ms
8,004 KB
testcase_26 AC 18 ms
8,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    H, W = map(int, input().split())
    s = [input() for i in range(H)]
    return H, W, s

def parse_board(H, W, s):
    reds = [False] * (H * W)
    start = -1
    goal = -1
    for h in range(H):
        sh = s[h]
        for w in range(W):
            c = sh[w]
            if c == 'S':
                start = h * W + w
            elif c == 'G':
                goal = h * W + w
            elif c == 'R':
                reds[h * W + w] = True
    return reds, start, goal

def solve(H, W, s):
    next_move = [(), ()]
    next_move[0] = ((-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1))
    next_move[1] = ((-1, -1), (-1, 1), (1, -1), (1, 1))
    reds, start, goal = parse_board(H, W, s)
    return wfs(start, goal, reds, H, W, next_move)

def wfs(start, goal, reds, H, W, next_move):
    HW = H * W
    dist = [-1] * (HW * 2)
    goals = [goal, goal + HW]
    steps = 0
    state = 0
    frontiers = [start]
    dist[start] = 0
    while frontiers:
        steps += 1
        new_frontiers = []
        for spos in frontiers:
            state, pos = divmod(spos, HW)
            h, w = divmod(pos, W)
            for dh, dw in next_move[state]:
                nh = h + dh
                nw = w + dw
                if 0 <= nh < H and 0 <= nw < W:
                    npos = nh * W + nw
                    new_state = 1 - state if reds[npos] else state
                    nspos = new_state * HW + npos
                    if dist[nspos] == -1:
                        dist[nspos] = steps
                        new_frontiers.append(nspos)
                        if nspos in goals:
                            return steps
        frontiers = new_frontiers
    return -1

if __name__ == '__main__':
    pars = read_data()
    print(solve(*pars))
0