結果
| 問題 | No.1598 4×4 Grid | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-07-09 23:59:15 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 489 ms / 4,000 ms | 
| コード長 | 1,843 bytes | 
| コンパイル時間 | 1,988 ms | 
| コンパイル使用メモリ | 200,332 KB | 
| 最終ジャッジ日時 | 2025-01-22 23:02:34 | 
| ジャッジサーバーID (参考情報) | judge4 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 7 | 
ソースコード
#include <bits/stdc++.h>
using ll = long long;
using std::cin;
using std::cout;
using std::endl;
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
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; }
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;
void solve()
{
  int K; cin >> K;
  const int N = 16;
  std::vector dp(1 << N, std::vector<ll>(216 * 2 + 1));
  dp[0][216] = 1;
  auto enc = [&](const int h, const int w)
  {
    return h * 4 + w;
  };
  auto dec = [&](const int S)
  {
    return std::make_pair(S / 4 , S % 4);
  };
  const int dh[] = {-1, 0, 1, 0};
  const int dw[] = {0, -1, 0, 1};
  for (int S = 0; S < (1 << N); ++S)
  {
    int nxt = 1;
    for (int i = 0; i < N; ++i)
    {
      nxt += (S >> i & 1);
    }
    for (int s = 0; s <= 216 * 2; ++s)
    {
      if(dp[S][s] == 0)
        continue;
      for (int i = 0; i < N; ++i)
      {
        if(~S >> i & 1)
        {
          int ns = s;
          const auto [h, w] = dec(i);
          for (int k = 0; k < 4; ++k)
          {
            const int nh = h + dh[k];
            const int nw = w + dw[k];
            if(nh < 0 or nh >= 4 or nw < 0 or nw >= 4)
              continue;
            if(S >> enc(nh, nw) & 1)
            {
              ns += nxt;
            }
            else
            {
              ns -= nxt;
            }
          }
          if(ns >= 0 and ns <= 216 * 2)
          {
            dp[S | (1 << i)][ns] += dp[S][s];
          }
        }
      }
    }
  }
  cout << dp[(1 << N) - 1][216 + K] << "\n";
}
int main()
{
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  int kkt = 1; 
  // cin >> kkt;
  while(kkt--)
    solve();
  return 0;
}
            
            
            
        