結果

問題 No.1598 4×4 Grid
ユーザー tnakao0123tnakao0123
提出日時 2021-07-12 14:45:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 4,000 ms
コード長 1,792 bytes
コンパイル時間 706 ms
コンパイル使用メモリ 90,900 KB
実行使用メモリ 115,300 KB
最終ジャッジ日時 2023-09-14 20:34:24
合計ジャッジ時間 2,355 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
114,832 KB
testcase_01 AC 85 ms
115,084 KB
testcase_02 AC 88 ms
115,300 KB
testcase_03 AC 104 ms
115,156 KB
testcase_04 AC 110 ms
115,184 KB
testcase_05 AC 127 ms
115,168 KB
testcase_06 AC 132 ms
115,096 KB
testcase_07 AC 12 ms
4,380 KB
testcase_08 AC 85 ms
115,172 KB
testcase_09 AC 112 ms
115,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1598.cc:  No.1598 4×4 Grid - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int N = 4;
const int NN = N * N;
const int NNBITS = 1 << NN;
const int MAX_K = 216;
const int dxs[] = { 1, 0, -1, 0 }, dys[] = { 0, -1, 0, 1 };

/* typedef */

typedef long long ll;

/* global variables */

int bnums[NNBITS], nbs[NN], phs[NNBITS];
ll dp[NNBITS][MAX_K + 1];

/* subroutines */

inline int xy2p(int x, int y) { return y * N + x; }
inline void p2xy(int p, int &x, int &y) { x = p % N, y = p / N; }

/* main */

int main() {
  bnums[0] = 0;
  for (int bits = 1, msb = 1; bits < NNBITS; bits++) {
    if ((msb << 1) <= bits) msb <<= 1;
    bnums[bits] = bnums[bits ^ msb] + 1;
  }

  for (int u = 0; u < NN; u++) {
    int x, y;
    p2xy(u, x, y);
    for (int di = 0; di < 4; di++) {
      int vx = x + dxs[di], vy = y + dys[di];
      if (vx >= 0 && vx < N && vy >= 0 && vy < N)
	nbs[u] |= (1 << xy2p(vx, vy));
    }
  }

  for (int bits = 0; bits < NNBITS; bits++)
    for (int i = 0, bi = 1; i < NN; i++, bi <<= 1)
      if (bits & bi)
	phs[bits] += bnums[nbs[i]] - bnums[bits & nbs[i]];
    
  int k;
  scanf("%d", &k);

  dp[0][0] = 1;

  for (int bits = 0; bits < NNBITS; bits++)
    for (int j = 0, bj = 1; j < NN; j++, bj <<= 1)
      if (! (bits & bj)) {
	int bits1 = (bits | bj);
	int di1 = phs[bits1];
	for (int i = 0; i + di1 <= k; i++)
	  dp[bits1][i + di1] += dp[bits][i];
      }

  printf("%lld\n", dp[NNBITS - 1][k]);
  return 0;
}
0