結果

問題 No.861 ケーキカット
ユーザー face4face4
提出日時 2019-08-09 23:12:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 1,000 ms
コード長 1,920 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 74,644 KB
実行使用メモリ 7,356 KB
最終ジャッジ日時 2023-09-22 02:14:07
合計ジャッジ時間 2,086 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
7,080 KB
testcase_01 AC 21 ms
7,284 KB
testcase_02 AC 21 ms
7,088 KB
testcase_03 AC 22 ms
7,084 KB
testcase_04 AC 22 ms
7,092 KB
testcase_05 AC 22 ms
7,080 KB
testcase_06 AC 21 ms
7,356 KB
testcase_07 AC 21 ms
7,096 KB
testcase_08 AC 22 ms
7,092 KB
testcase_09 AC 21 ms
7,156 KB
testcase_10 AC 22 ms
7,084 KB
testcase_11 AC 22 ms
7,080 KB
testcase_12 AC 22 ms
7,084 KB
testcase_13 AC 22 ms
7,244 KB
testcase_14 AC 22 ms
7,096 KB
testcase_15 AC 22 ms
7,088 KB
testcase_16 AC 22 ms
7,080 KB
testcase_17 AC 22 ms
7,252 KB
testcase_18 AC 22 ms
7,180 KB
testcase_19 AC 23 ms
7,156 KB
testcase_20 AC 22 ms
7,080 KB
testcase_21 AC 22 ms
7,084 KB
testcase_22 AC 21 ms
7,140 KB
testcase_23 AC 22 ms
7,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<queue>
using namespace std;
typedef long long ll;

#define inRange(x,a,b) (a <= x && x < b)
int di[8] = {0,0,1,-1,1,1,-1,-1};
int dj[8] = {1,-1,0,0,1,-1,1,-1};

ll c[5][5];
bool f[5][5] = {}, memo[1<<25] = {};
ll sum = 0, ans = 1ll<<60;

void dfs(int val, ll acc){
    memo[val] = true;
    ans = min(ans, abs(sum-acc-acc));

    for(int x = 0; x < 25; x++){
        int i = x/5, j = x%5;
        if(f[i][j] || memo[val+(1<<x)] || val+(1<<x)==(1<<25)-1) continue;
        bool ok = false;
        for(int k = 0; k < 4; k++){
            int ni = i+di[k], nj = j+dj[k];
            if(inRange(ni,0,5)&&inRange(nj,0,5)&&f[ni][nj]) ok = true;
        }
        if(!ok) continue;

        f[i][j] = true;
        
        queue<int> q;
        bool tmp[5][5] = {};
        bool flag = true;
        for(int ii = 0; ii < 5; ii++){
            for(int jj = 0; jj < 5; jj++){
                if(f[ii][jj]) continue;
                tmp[ii][jj] = true;
                q.push(ii*5+jj);
                flag = false;
                break;
            }
            if(!flag)   break;
        }
        int dual = 0;
        while(!q.empty()){
            int p = q.front(); q.pop();
            int ii = p/5, jj = p%5;
            dual += 1<<p;
            for(int k = 0; k < 4; k++){
                int ni = ii+di[k], nj = jj+dj[k];
                if(inRange(ni,0,5)&&inRange(nj,0,5)&&!tmp[ni][nj]&&!f[ni][nj]){
                    tmp[ni][nj] = true;
                    q.push(ni*5+nj);
                }
            }
        }

        if(dual + val + (1<<x) == (1<<25)-1){
            dfs(val+(1<<x), acc+c[i][j]);
        }
        f[i][j] = false;
    }
}

int main(){
    for(int i = 0; i < 5; i++){
        for(int j = 0; j < 5; j++){
            cin >> c[i][j];
            sum += c[i][j];
        }
    }
    f[0][0] = true;
    dfs(1, c[0][0]);
    cout << ans << endl;
    return 0;
}
0