結果
| 問題 | 
                            No.861 ケーキカット
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2019-08-10 17:38:37 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 442 ms / 1,000 ms | 
| コード長 | 2,503 bytes | 
| コンパイル時間 | 1,092 ms | 
| コンパイル使用メモリ | 129,880 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-07 19:07:07 | 
| 合計ジャッジ時間 | 12,509 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 21 | 
ソースコード
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;
const int N = 5;
const long long INF = LLONG_MAX / 2;
const int dy[] = {0, 1, 0, -1};
const int dx[] = {-1, 0, 1, 0};
bool check(const vector<vector<int> >& cake)
{
    for(int i=0; i<2; ++i){
        int sy = -1;
        int sx = -1;
        for(int y=0; y<N; ++y){
            for(int x=0; x<N; ++x){
                if(cake[y][x] == i){
                    sy = y;
                    sx = x;
                }
            }
        }
        if(sy == -1)
            continue;
        vector<vector<bool> > used(N, vector<bool>(N, false));
        queue<pair<int, int> > q;
        used[sy][sx] = true;
        q.push(make_pair(sy, sx));
        while(!q.empty()){
            int y, x;
            tie(y, x) = q.front();
            q.pop();
            for(int j=0; j<4; ++j){
                int y2 = y + dy[j];
                int x2 = x + dx[j];
                if(!(0 <= y2 && y2 < N && 0 <= x2 && x2 < N))
                    continue;
                if(!used[y2][x2] && (cake[y2][x2] == i || cake[y2][x2] == -1)){
                    used[y2][x2] = true;
                    q.push(make_pair(y2, x2));
                }
            }
        }
        for(int y=0; y<N; ++y){
            for(int x=0; x<N; ++x){
                if(cake[y][x] == i && !used[y][x])
                    return false;
            }
        }
    }
    return true;
}
long long solve(const vector<int>& val, vector<vector<int> >& cake, int k, long long diff)
{
    if(!check(cake))
        return INF;
    if(k == N * N)
        return abs(diff);
    long long ans = INF;
    for(int i=0; i<2; ++i){
        long long diff2 = diff + val[k] * (i == 0 ? 1 : -1);
        cake[k/5][k%5] = i;
        ans = min(ans, solve(val, cake, k+1, diff2));
        cake[k/5][k%5] = -1;
    }
    return ans;
}
int main()
{
    vector<int> val(N*N);
    for(int i=0; i<N*N; ++i)
        cin >> val[i];
    vector<vector<int> > cake(N, vector<int>(N, -1));
    cout << solve(val, cake, 0, 0) << endl;
    return 0;
}