結果

問題 No.1598 4×4 Grid
ユーザー milanis48663220milanis48663220
提出日時 2021-07-09 22:35:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 596 ms / 4,000 ms
コード長 2,437 bytes
コンパイル時間 1,299 ms
コンパイル使用メモリ 124,932 KB
実行使用メモリ 313,440 KB
最終ジャッジ日時 2023-09-14 09:54:28
合計ジャッジ時間 8,582 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 589 ms
313,292 KB
testcase_01 AC 590 ms
313,200 KB
testcase_02 AC 589 ms
313,440 KB
testcase_03 AC 595 ms
313,224 KB
testcase_04 AC 591 ms
313,268 KB
testcase_05 AC 586 ms
313,196 KB
testcase_06 AC 596 ms
313,268 KB
testcase_07 AC 593 ms
313,272 KB
testcase_08 AC 593 ms
313,196 KB
testcase_09 AC 595 ms
313,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template <typename T>
class OffsetVec{
    public:
	int n;
    vector<T> v;
	T& operator[](int x) {
        return v[x+n];
	}
    void print(){
        for(int i = -n; i <= n; i++) cout << v[i+n] << ' ';
        cout << endl;
    }
    OffsetVec(int _n){
        n = _n;
        v = vector<T>(2*n+1);
    }
    OffsetVec(int _n, T x){
        n = _n;
        v = vector<T>(2*n+1, x);
    }
};

const int N_MAX = 1<<16;
const int m = 300;

int popcount(int n){
    int ans = 0;
    for(int i = 0; i < 16; i++){
        if(n&(1<<i)) ans++;
    }
    return ans;
}

int dh[4] = {0, 0, -1, 1};
int dw[4] = {-1, 1, 0, 0};

bool is_in_field(int i, int j){
    return i >= 0 && i < 4 && j >= 0 && j < 4;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int k; cin >> k;
    vector<OffsetVec<ll>> dp(N_MAX, OffsetVec<ll>(m, 0));
    dp[0][0] = 1;
    ll sum = 0;
    for(int s = 0; s < (1<<16); s++){
        int nx = popcount(s)+1;
        vector<vector<bool>> used(4, vector<bool>(4));
        for(int j = 0; j < 16; j++){
            if(s&(1<<j)) used[j/4][j%4] = true;
        }
        for(int j = 0; j < 16; j++){
            if(s&(1<<j)) continue;
            int x = j/4, y = j%4;
            int cnt = 0;
            for(int k = 0; k < 4; k++){
                int xto = x+dh[k], yto = y+dw[k];
                if(is_in_field(xto, yto)){
                    if(used[xto][yto]) cnt++;
                    else cnt--;
                }
            }
            for(int k = -m; k <= m; k++){
                if(dp[s][k] == 0) continue;
                assert(k+cnt*nx >= -m);
                assert(k+cnt*nx <= m);
                dp[s+(1<<j)][k+cnt*nx] += dp[s][k];
            }
        }
    }
    cout << dp[(1<<16)-1][k] << endl;
}
0