結果

問題 No.2675 KUMA
ユーザー t98slider
提出日時 2024-03-15 22:48:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,983 bytes
コンパイル時間 2,412 ms
コンパイル使用メモリ 208,020 KB
最終ジャッジ日時 2025-02-20 05:44:22
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

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