結果

問題 No.1598 4×4 Grid
ユーザー startcppstartcpp
提出日時 2021-07-12 08:18:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 329 ms / 4,000 ms
コード長 1,879 bytes
コンパイル時間 471 ms
コンパイル使用メモリ 64,704 KB
実行使用メモリ 225,244 KB
最終ジャッジ日時 2023-09-14 20:26:53
合計ジャッジ時間 4,324 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 329 ms
225,036 KB
testcase_01 AC 283 ms
225,100 KB
testcase_02 AC 286 ms
225,244 KB
testcase_03 AC 290 ms
225,112 KB
testcase_04 AC 284 ms
225,172 KB
testcase_05 AC 283 ms
225,124 KB
testcase_06 AC 285 ms
225,044 KB
testcase_07 AC 287 ms
225,196 KB
testcase_08 AC 288 ms
225,072 KB
testcase_09 AC 283 ms
225,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//1から順番に埋めると、何を埋めたか忘れても、+, ーどちらに寄与するかは決まる。
//ケアレスミス
//1. 一時的にスコアが負になるケースの見落とし -> 変形したことを忘れない
//2. ny, nxの範囲外チェック忘れた
//3. 漸化式のjとkが逆(dp[i][j]を足していた) -> 添字の意味に注意
//4. continueするべきところがbreak -> 1.とともに修正する必要があった
//デバッグ
//2 x 2のケースでやった。
#include <iostream>
#define int long long
using namespace std;

int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
const int N = 4;
const int C = 216;
int dp[1 << 16][2 * C + 1];

signed main() {
	int N2 = N * N;

	dp[0][C] = 1;
	for (int i = 0; i < (1 << N2); i++) {
		int bitCnt = 0;
		for (int j = 0; j < N2; j++) {
			if ((i >> j) % 2 == 1) bitCnt++;
		}
		//値bitCntを入れる場面
		for (int j = 0; j < N2; j++) { //場所jに入れる
			if ((i >> j) % 2 == 1) continue;
			int y = j / N;
			int x = j % N;
			int keisu = 0;
			for (int dir = 0; dir < 4; dir++) {
				int ny = y + dy[dir];
				int nx = x + dx[dir];
				if (ny < 0 || ny >= N || nx < 0 || nx >= N) { continue; }
				int pos = ny * N + nx;
				if ((i >> pos) & 1) keisu++;
				else keisu--;
			}
			int addScore = keisu * bitCnt;
			/*for (int k = 0; k < N; k++) {
				for (int l = 0; l < N; l++) {
					cout << (i >> (k * N + l)) % 2;
				}
				cout << endl;
			}
			cout << "y, x = " << y << ", " << x << ", keisu = " << keisu << endl;*/

			for (int k = 0; k <= 2 * C; k++) {
				if (k + addScore < 0 || k + addScore > 2 * C) continue;
				//cout << "dp[" << i << ", " << k << "] -> dp[" << i + (1 << j) << ", " << k + addScore << "]" << endl;;
				dp[i + (1 << j)][k + addScore] += dp[i][k];
			}
		}
	}

	int K;
	cin >> K;
	cout << dp[(1 << N2) - 1][C + K] << endl;

	return 0;
}
0