結果
| 問題 |
No.1598 4×4 Grid
|
| コンテスト | |
| ユーザー |
startcpp
|
| 提出日時 | 2021-07-12 08:18:33 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 460 ms / 4,000 ms |
| コード長 | 1,879 bytes |
| コンパイル時間 | 586 ms |
| コンパイル使用メモリ | 65,832 KB |
| 実行使用メモリ | 225,152 KB |
| 最終ジャッジ日時 | 2024-07-02 03:19:24 |
| 合計ジャッジ時間 | 5,934 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 7 |
ソースコード
//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;
}
startcpp