結果

問題 No.2675 KUMA
ユーザー 👑 eoeoeoeo
提出日時 2024-03-04 22:21:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 1,915 bytes
コンパイル時間 2,802 ms
コンパイル使用メモリ 213,600 KB
実行使用メモリ 8,896 KB
最終ジャッジ日時 2024-03-04 22:21:57
合計ジャッジ時間 5,030 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,260 KB
testcase_01 AC 5 ms
8,260 KB
testcase_02 AC 5 ms
8,260 KB
testcase_03 AC 4 ms
8,260 KB
testcase_04 AC 4 ms
8,260 KB
testcase_05 AC 29 ms
8,892 KB
testcase_06 AC 25 ms
8,892 KB
testcase_07 AC 4 ms
8,260 KB
testcase_08 AC 32 ms
8,644 KB
testcase_09 AC 14 ms
8,636 KB
testcase_10 AC 60 ms
8,892 KB
testcase_11 AC 56 ms
8,892 KB
testcase_12 AC 12 ms
8,388 KB
testcase_13 AC 5 ms
8,260 KB
testcase_14 AC 5 ms
8,388 KB
testcase_15 AC 4 ms
8,260 KB
testcase_16 AC 34 ms
8,896 KB
testcase_17 AC 4 ms
8,260 KB
testcase_18 AC 4 ms
8,260 KB
testcase_19 AC 4 ms
8,260 KB
testcase_20 AC 5 ms
8,260 KB
testcase_21 AC 5 ms
8,260 KB
testcase_22 AC 4 ms
8,260 KB
testcase_23 AC 5 ms
8,260 KB
testcase_24 AC 4 ms
8,260 KB
testcase_25 AC 5 ms
8,260 KB
testcase_26 AC 4 ms
8,260 KB
testcase_27 AC 4 ms
8,260 KB
testcase_28 AC 5 ms
8,260 KB
testcase_29 AC 4 ms
8,260 KB
testcase_30 AC 5 ms
8,388 KB
testcase_31 AC 5 ms
8,388 KB
testcase_32 AC 5 ms
8,388 KB
testcase_33 AC 5 ms
8,388 KB
testcase_34 AC 5 ms
8,388 KB
testcase_35 AC 5 ms
8,388 KB
testcase_36 AC 6 ms
8,388 KB
testcase_37 AC 6 ms
8,388 KB
testcase_38 AC 9 ms
8,388 KB
testcase_39 AC 8 ms
8,388 KB
testcase_40 AC 11 ms
8,388 KB
testcase_41 AC 12 ms
8,388 KB
testcase_42 AC 13 ms
8,388 KB
testcase_43 AC 12 ms
8,388 KB
testcase_44 AC 13 ms
8,388 KB
testcase_45 AC 22 ms
8,636 KB
testcase_46 AC 21 ms
8,640 KB
testcase_47 AC 21 ms
8,636 KB
testcase_48 AC 36 ms
8,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// writer

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;

    vector<int> x(N), y(N);

    // その座標にいる UMA の集合を bit で持っておく。
    int uma_set[1100][1100] = {};
    for (int i = 0; i < N; i++) {
        cin >> x[i] >> y[i];
        // 負になってバグるのを防ぐため +5 ずつしておく
        x[i] += 5;
        y[i] += 5;
        uma_set[x[i]][y[i]] = (1 << i);
    }

    // 設置する点の候補を列挙
    set<pair<int, int>> candidates;
    const int dx[] = {2, 1, -1, -2, -2, -1, 1, 2};
    const int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < 8; j++) {
            candidates.insert({x[i] + dx[j], y[i] + dy[j]});
        }
    }
    // UMA のいる場所を除く
    for (int i = 0; i < N; i++) {
        pair<int, int> p = {x[i], y[i]};
        if (candidates.count(p)) candidates.erase(p);
    }

    // dp
    vector<int> dp(1 << N, numeric_limits<int>::max());
    dp[0] = 0;
    for (auto &&[a, b] : candidates) {
        auto new_dp = dp;

        for (int bits = 0; bits < (1 << N); bits++) {
            if (dp[bits] == numeric_limits<int>::max()) continue;

            const int dx1[] = {-1, -1, +2, -2};
            const int dy1[] = {+2, -2, -1, -1};
            const int dx2[] = {+1, +1, +2, -2};
            const int dy2[] = {+2, -2, +1, +1};

            for (int dir = 0; dir < 4; dir++) {
                int 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.swap(new_dp);
    }

    cout << (dp.back() != numeric_limits<int>::max() ? dp.back() : -1) << endl;
}
0