結果

問題 No.2675 KUMA
ユーザー 👑 nu50218nu50218
提出日時 2024-02-08 17:51:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 1,571 bytes
コンパイル時間 160 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 124,928 KB
最終ジャッジ日時 2024-02-13 20:55:57
合計ジャッジ時間 6,434 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
69,584 KB
testcase_01 AC 66 ms
85,264 KB
testcase_02 AC 43 ms
69,584 KB
testcase_03 AC 44 ms
69,584 KB
testcase_04 AC 44 ms
69,584 KB
testcase_05 AC 225 ms
100,644 KB
testcase_06 AC 213 ms
96,488 KB
testcase_07 AC 44 ms
69,584 KB
testcase_08 AC 271 ms
100,152 KB
testcase_09 AC 121 ms
93,976 KB
testcase_10 AC 237 ms
106,808 KB
testcase_11 AC 186 ms
105,460 KB
testcase_12 AC 94 ms
86,160 KB
testcase_13 AC 51 ms
73,104 KB
testcase_14 AC 89 ms
85,392 KB
testcase_15 AC 44 ms
69,584 KB
testcase_16 AC 220 ms
124,928 KB
testcase_17 AC 44 ms
69,584 KB
testcase_18 AC 43 ms
69,584 KB
testcase_19 AC 43 ms
69,584 KB
testcase_20 AC 44 ms
69,584 KB
testcase_21 AC 44 ms
69,584 KB
testcase_22 AC 49 ms
72,340 KB
testcase_23 AC 49 ms
72,340 KB
testcase_24 AC 50 ms
72,468 KB
testcase_25 AC 51 ms
72,848 KB
testcase_26 AC 50 ms
72,464 KB
testcase_27 AC 52 ms
73,360 KB
testcase_28 AC 51 ms
73,104 KB
testcase_29 AC 50 ms
73,104 KB
testcase_30 AC 56 ms
73,616 KB
testcase_31 AC 66 ms
85,392 KB
testcase_32 AC 66 ms
85,392 KB
testcase_33 AC 69 ms
85,392 KB
testcase_34 AC 65 ms
85,392 KB
testcase_35 AC 69 ms
85,528 KB
testcase_36 AC 85 ms
85,528 KB
testcase_37 AC 81 ms
85,528 KB
testcase_38 AC 87 ms
85,652 KB
testcase_39 AC 86 ms
85,652 KB
testcase_40 AC 95 ms
85,652 KB
testcase_41 AC 100 ms
86,160 KB
testcase_42 AC 105 ms
86,288 KB
testcase_43 AC 102 ms
86,032 KB
testcase_44 AC 120 ms
86,416 KB
testcase_45 AC 153 ms
97,688 KB
testcase_46 AC 145 ms
97,560 KB
testcase_47 AC 140 ms
99,096 KB
testcase_48 AC 203 ms
107,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    N = int(sys.stdin.readline())

    x = [0] * N
    y = [0] * N

    # その座標にいる UMA の集合を bit で持っておく。
    uma_set = [[0] * 1100 for _ in range(1100)]
    for i in range(N):
        x[i], y[i] = map(int, sys.stdin.readline().split())
        # 負になってバグるのを防ぐため +5 ずつしておく
        x[i] += 5
        y[i] += 5
        uma_set[x[i]][y[i]] = 1 << i

    # 設置する点の候補を列挙
    dx = [2, 1, -1, -2, -2, -1, 1, 2]
    dy = [1, 2, 2, 1, -1, -2, -2, -1]
    candidates = {(x[i] + dx[j], y[i] + dy[j]) for i in range(N) for j in range(8)}
    # UMA のいる場所を除く
    candidates -= set(zip(x, y))

    # dp
    INF = 1 << 30

    dp = [INF] * (1 << N)
    new_dp = deque([INF] * (1 << N))
    dp[0] = 0

    dx1 = [-1, -1, +2, -2]
    dy1 = [+2, -2, -1, -1]
    dx2 = [+1, +1, +2, -2]
    dy2 = [+2, -2, +1, +1]

    for a, b in candidates:
        new_dp = dp.copy()

        for bits in range(1 << N):
            if dp[bits] == INF:
                continue

            for dir in range(4):
                bits_next = bits

                # 向いた先の UMA を bits_next に加える
                bits_next |= uma_set[a + dx1[dir]][b + dy1[dir]]
                bits_next |= uma_set[a + dx2[dir]][b + dy2[dir]]

                new_dp[bits_next] = min(new_dp[bits_next], dp[bits] + 1)
            
        dp, new_dp = new_dp, dp

    print((dp[-1] if dp[-1] != INF else -1))

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