結果
| 問題 | 
                            No.861 ケーキカット
                             | 
                    
| コンテスト | |
| ユーザー | 
                             merom686
                         | 
                    
| 提出日時 | 2019-08-10 01:06:53 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 58 ms / 1,000 ms | 
| コード長 | 2,668 bytes | 
| コンパイル時間 | 701 ms | 
| コンパイル使用メモリ | 80,372 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-07 19:05:35 | 
| 合計ジャッジ時間 | 2,905 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 21 | 
ソースコード
#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;
}
            
            
            
        
            
merom686