結果

問題 No.367 ナイトの転身
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-04-29 23:32:56
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,792 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,548 KB
実行使用メモリ 277,376 KB
最終ジャッジ日時 2024-04-15 06:00:24
合計ジャッジ時間 3,998 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
62,408 KB
testcase_01 AC 42 ms
56,252 KB
testcase_02 AC 42 ms
55,624 KB
testcase_03 AC 44 ms
55,352 KB
testcase_04 AC 52 ms
64,264 KB
testcase_05 AC 43 ms
55,228 KB
testcase_06 AC 46 ms
57,136 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
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 #

#!/usr/bin/env pypy3

import collections
import itertools


DS_KNIGHT = [(1, 2), (-1, -2), (1, -2), (-1, 2), (2, 1), (-2, -1), (2, -1), (-2, 1)]
DS_MB = [(1, 1), (-1, -1), (1, -1), (-1, 1)]
INF = 10 ** 8
Position = collections.namedtuple("Position", "r c")
Status = collections.namedtuple("Status", "is_knight position")


def min_num_of_operations(height, width, board, start, goal):
    s_start = Status(True, start)
    dist = collections.defaultdict(lambda: INF)
    dist[s_start] = 0
    q = collections.deque()
    q.append(s_start)
    while q:
        s0 = q.popleft()
        if s0.position == goal:
            break
        for dr, dc in (DS_KNIGHT if s0.is_knight else DS_MB):
            p = Position(s0.position.r + dr, s0.position.c + dc)
            if p.r < 0 or p.r >= height:
                continue
            elif p.c < 0 or p.c >= width:
                continue
            elif dist[Status(True, p)] >= INF or dist[Status(False, p)] >= INF:
                new_class = not s0.is_knight if board[p.r][p.c] == "R" else s0.is_knight
                s = Status(new_class, p)
                dist[s] = dist[s0] + 1
                q.append(s)
    return min(dist[Status(True, goal)], dist[Status(False, goal)])


def main():
    height, width = map(int, input().split())
    board = [input() for _ in range(height)]
    start = None
    goal = None
    for r, c in itertools.product(range(height), range(width)):
        if board[r][c] == "S":
            start = Position(r, c)
        elif board[r][c] == "G":
            goal = Position(r, c)
        if (start is not None) and (goal is not None):
            break
    answer = min_num_of_operations(height, width, board, start, goal)
    print(answer if answer < INF else -1)


if __name__ == '__main__':
    main()
0