結果

問題 No.861 ケーキカット
ユーザー merom686merom686
提出日時 2019-08-10 01:06:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 1,000 ms
コード長 2,668 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 80,172 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 02:16:59
合計ジャッジ時間 3,146 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
4,376 KB
testcase_01 AC 62 ms
4,376 KB
testcase_02 AC 62 ms
4,380 KB
testcase_03 AC 63 ms
4,380 KB
testcase_04 AC 63 ms
4,376 KB
testcase_05 AC 62 ms
4,380 KB
testcase_06 AC 62 ms
4,380 KB
testcase_07 AC 64 ms
4,376 KB
testcase_08 AC 63 ms
4,380 KB
testcase_09 AC 63 ms
4,376 KB
testcase_10 AC 63 ms
4,376 KB
testcase_11 AC 63 ms
4,380 KB
testcase_12 AC 62 ms
4,380 KB
testcase_13 AC 63 ms
4,380 KB
testcase_14 AC 63 ms
4,376 KB
testcase_15 AC 64 ms
4,376 KB
testcase_16 AC 62 ms
4,376 KB
testcase_17 AC 64 ms
4,376 KB
testcase_18 AC 64 ms
4,376 KB
testcase_19 AC 63 ms
4,376 KB
testcase_20 AC 63 ms
4,380 KB
testcase_21 AC 63 ms
4,376 KB
testcase_22 AC 63 ms
4,376 KB
testcase_23 AC 63 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

int main() {
    constexpr int N = 5;

    int c[N][N];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cin >> c[i][j];
        }
    }

    int p[16];
    for (int h = 0; h < 4; h++) {
        p[h] = h;
        p[h + 4] = 4 + h * N;
        p[h + 8] = 24 - h;
        p[h + 12] = 20 - h * N;
    }

    int d[N][N];
    int d0;
    ll s;
    int x;
    function<void(int, int)> f = [&](int i, int j) {
        if (d[i][j] != d0) return;
        s += c[i][j];
        x++;
        d[i][j] ^= 4;
        if (i > 0) f(i - 1, j);
        if (i < N - 1) f(i + 1, j);
        if (j > 0) f(i, j - 1);
        if (j < N - 1) f(i, j + 1);
    };

    ll r = 1LL << 60;
    for (int k = 0; k < 16; k++) {
        for (int l = 1; l <= 16; l++) {
            for (int h = 0; h < 16; h++) {
                d[0][p[(k + h) % 16]] = h < l;
            }
            for (int h = 0; h < 1 << 9; h++) {
                for (int i = 0; i < 3; i++) {
                    for (int j = 0; j < 3; j++) {
                        d[i + 1][j + 1] = h >> (i * 3 + j) & 1;
                    }
                }
                s = 0;
                x = 0;
                for (int a = 0; a < 2; a++) {
                    int e = p[k];
                    if (a == 0) {
                        e = -1;
                        for (int h = 0; h < N * N; h++) {
                            if ((&d[0][0])[h] == 0) {
                                e = h;
                                break;
                            }
                        }
                        if (e < 0) continue;
                    }
                    int i = e / N, j = e % N;
                    d0 = a;
                    f(i, j);
                    s *= -1;
                }

                //if (k == 0 && l == 16) {
                //    cout << x << '\n';
                //    for (int i = 0; i < N; i++) {
                //        for (int j = 0; j < N; j++) {
                //            cout << d[i][j] << ' ';
                //        }
                //        cout << '\n';
                //    }
                //    cout << '\n';
                //}
                if (x == N * N) r = min(r, abs(s));

                for (int i = 0; i < N; i++) {
                    for (int j = 0; j < N; j++) {
                        d[i][j] &= ~4;
                    }
                }
            }
        }
    }

    cout << r << endl;

    return 0;
}
0