結果

問題 No.2675 KUMA
ユーザー t98slidert98slider
提出日時 2024-03-15 22:48:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,983 bytes
コンパイル時間 2,394 ms
コンパイル使用メモリ 214,808 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-15 22:48:36
合計ジャッジ時間 3,973 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

template<class T> ostream& operator << (ostream& os, const vector<T>& vec) {
    if(vec.empty()) return os;
    os << vec[0];
    for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it;
    return os;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<pair<int,int>> p(n), p2, dir = {{2, 1}, {-2, 1}, {1, 2}, {1, -2}};
    for(auto &&[x, y] : p){
        cin >> x >> y;
        //cerr << x << " " << y << '\n';
        for(int i = 0; i < 4; i++){
            p2.emplace_back((i >> 1 & 1 ? -2 : 2) + x, (i & 1 ? -1 : 1) + y);
            p2.emplace_back((i & 1 ? -1 : 1) + x, (i >> 1 & 1 ? -2 : 2) + y);
        }
    }
    sort(p2.begin(), p2.end());
    p2.erase(unique(p2.begin(), p2.end()), p2.end());
    sort(p.begin(), p.end());

    vector<int> dp(1 << n, 1 << 30);
    dp[0] = 0;
    for(auto &&[x, y] : p2){
        if(binary_search(p.begin(), p.end(), make_pair(x, y))) continue;
        vector<int> ndp = dp;
        for(auto &&[dy, dx] : dir){
            int y1 = dy + y, x1 = dx + x;
            int y2 = (dy == 1 ? -dy : dy) + y, x2 = (dx == 1 ? -dx : dx) + x;
            int S = 0;
            int j = lower_bound(p.begin(), p.end(), make_pair(x1, y1)) - p.begin();
            if(j < n && x1 == p[j].first && y1 == p[j].second){
                S |= 1 << j;
            }
            j = lower_bound(p.begin(), p.end(), make_pair(x2, y2)) - p.begin();
            if(j < n && x2 == p[j].first && y2 == p[j].second){
                S |= 1 << j;
            }
            if(S == 0) continue;
            for(int U = (1 << n) - 1; U >= 0; U--){
                ndp[U | S] = min(ndp[U | S], dp[U] + 1);
            }
        }
        swap(dp, ndp);
    }
    //cerr << dp << '\n';
    cout << (dp.back() >> 30 & 1 ? -1 : dp.back()) << '\n';
}
0