結果

問題 No.2675 KUMA
ユーザー NokonoKotlinNokonoKotlin
提出日時 2024-03-13 22:34:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 1,588 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 124,924 KB
最終ジャッジ日時 2024-03-13 22:34:29
合計ジャッジ時間 6,359 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
69,584 KB
testcase_01 AC 71 ms
85,264 KB
testcase_02 AC 47 ms
69,584 KB
testcase_03 AC 46 ms
69,584 KB
testcase_04 AC 47 ms
69,584 KB
testcase_05 AC 231 ms
100,648 KB
testcase_06 AC 197 ms
96,488 KB
testcase_07 AC 47 ms
69,584 KB
testcase_08 AC 297 ms
100,152 KB
testcase_09 AC 127 ms
93,976 KB
testcase_10 AC 242 ms
106,808 KB
testcase_11 AC 195 ms
105,468 KB
testcase_12 AC 101 ms
86,160 KB
testcase_13 AC 54 ms
73,104 KB
testcase_14 AC 71 ms
85,392 KB
testcase_15 AC 46 ms
69,584 KB
testcase_16 AC 239 ms
124,924 KB
testcase_17 AC 47 ms
69,584 KB
testcase_18 AC 47 ms
69,584 KB
testcase_19 AC 47 ms
69,584 KB
testcase_20 AC 51 ms
69,584 KB
testcase_21 AC 49 ms
69,584 KB
testcase_22 AC 52 ms
72,340 KB
testcase_23 AC 56 ms
72,340 KB
testcase_24 AC 53 ms
72,468 KB
testcase_25 AC 55 ms
72,848 KB
testcase_26 AC 53 ms
72,464 KB
testcase_27 AC 55 ms
73,360 KB
testcase_28 AC 55 ms
73,104 KB
testcase_29 AC 54 ms
73,104 KB
testcase_30 AC 56 ms
73,616 KB
testcase_31 AC 73 ms
85,392 KB
testcase_32 AC 72 ms
85,392 KB
testcase_33 AC 74 ms
85,520 KB
testcase_34 AC 77 ms
85,392 KB
testcase_35 AC 73 ms
85,528 KB
testcase_36 AC 90 ms
85,528 KB
testcase_37 AC 85 ms
85,528 KB
testcase_38 AC 93 ms
85,652 KB
testcase_39 AC 91 ms
85,652 KB
testcase_40 AC 101 ms
85,652 KB
testcase_41 AC 108 ms
86,160 KB
testcase_42 AC 112 ms
86,288 KB
testcase_43 AC 107 ms
86,032 KB
testcase_44 AC 115 ms
86,416 KB
testcase_45 AC 163 ms
97,688 KB
testcase_46 AC 157 ms
97,560 KB
testcase_47 AC 145 ms
99,096 KB
testcase_48 AC 204 ms
107,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/pypy3
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