結果

問題 No.1598 4×4 Grid
ユーザー kenskens
提出日時 2021-07-10 02:18:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 574 ms / 4,000 ms
コード長 979 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 203,308 KB
実行使用メモリ 271,892 KB
最終ジャッジ日時 2023-09-14 13:55:36
合計ジャッジ時間 9,081 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 569 ms
271,892 KB
testcase_01 AC 568 ms
271,704 KB
testcase_02 AC 561 ms
271,748 KB
testcase_03 AC 573 ms
271,844 KB
testcase_04 AC 573 ms
271,748 KB
testcase_05 AC 570 ms
271,704 KB
testcase_06 AC 568 ms
271,780 KB
testcase_07 AC 573 ms
271,808 KB
testcase_08 AC 574 ms
271,824 KB
testcase_09 AC 564 ms
271,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
constexpr int MAX = 16;
constexpr int BASE = 260;
constexpr int dx[4] = {0,1,0,-1};
constexpr int dy[4] = {1,0,-1,0};

int main(){
  int k;
  cin >> k;
  vector<vector<ll>> dp(1<<MAX,vector<ll>(2*BASE));
  dp[0][BASE] = 1;
  rep(bit,1<<MAX) {
    int bc = 0;
    rep(i,MAX) if(bit & (1<<i)) bc++;
    rep(s,2*BASE) {
      if(dp[bit][s] == 0) continue;
      rep(x,4) rep(y,4) {
        if(bit & (1<<(x*4+y))) continue;
        int cnt = 0, exi = 0;
        rep(dir,4) {
          int nx = x + dx[dir], ny = y + dy[dir];
          if(nx >= 0 and nx < 4 and ny >= 0 and ny < 4) {
            cnt++;
            if(bit & (1<<(nx*4+ny))) exi++;
          }
        }
        int res = s + exi*bc - (cnt-exi)*bc;
        if(res >= 0 and res < 2*BASE) dp[bit|(1<<(x*4+y))][res] += dp[bit][s];
      }
    }
  }
  cout << dp[(1<<MAX)-1][BASE+k] << endl;
  return 0;
}
0