結果

問題 No.861 ケーキカット
ユーザー mamekinmamekin
提出日時 2019-08-10 17:37:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,503 bytes
コンパイル時間 1,436 ms
コンパイル使用メモリ 128,700 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-26 23:29:53
合計ジャッジ時間 12,701 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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(k == N * N)
        return abs(diff);
    if(!check(cake))
        return INF;

    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;
}
0