結果

問題 No.1598 4×4 Grid
ユーザー 👑 jupirojupiro
提出日時 2021-07-09 23:59:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 433 ms / 4,000 ms
コード長 1,843 bytes
コンパイル時間 2,072 ms
コンパイル使用メモリ 205,732 KB
実行使用メモリ 227,000 KB
最終ジャッジ日時 2023-09-14 11:24:46
合計ジャッジ時間 7,436 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 430 ms
226,864 KB
testcase_01 AC 423 ms
226,920 KB
testcase_02 AC 424 ms
226,840 KB
testcase_03 AC 431 ms
227,000 KB
testcase_04 AC 432 ms
226,844 KB
testcase_05 AC 422 ms
226,924 KB
testcase_06 AC 422 ms
226,892 KB
testcase_07 AC 428 ms
226,840 KB
testcase_08 AC 426 ms
226,896 KB
testcase_09 AC 433 ms
226,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0