結果

問題 No.861 ケーキカット
ユーザー ttttan2ttttan2
提出日時 2019-08-10 00:54:33
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,835 bytes
コンパイル時間 1,254 ms
コンパイル使用メモリ 145,660 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-26 22:18:46
合計ジャッジ時間 33,639 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
//ios::sync_with_stdio(false);
//cin.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef tuple<ll,ll,ll> tl;
ll mod=1000000007;
ll mod2=998244353;
ll inf=1000000000000000000;
double pi=2*acos(0);
#define rep(i,m,n) for(int i=m;i<n;i++)
#define rrep(i,n,m) for(int i=n;i>=m;i--)
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
ll lmax(ll a,ll b){
    if(a<b)return b;
    else return a;
}
ll lmin(ll a,ll b){
    if(a<b)return a;
    else return b;
}
ll c[5][5];
ll sum;
ll ans;
bool used[5][5];
pll par[5][5];
void dfs(ll nh,ll nw,ll cnt){
    //cout<<nh<<" "<<nw<<"    ";
    ans=min(ans,abs(sum-cnt-cnt));
    rep(i,0,4){
        ll nnh=nh+dh[i];
        ll nnw=nw+dw[i];
        if(nnh<0||nnh>=5||nnw<0||nnw>=5)continue;
        if(par[nh][nw].first==nnh&&par[nh][nw].second==nnw){
            par[nh][nw]={-1,-1};
            dfs(nnh,nnw,cnt);
            continue;
        }
        if(used[nnh][nnw])continue;
        used[nnh][nnw]=true;
        par[nnh][nnw]={nh,nw};
        dfs(nnh,nnw,cnt+c[nnh][nnw]);
        used[nnh][nnw]=false;
        par[nnh][nnw]={-1,-1};
    }
}
int main(){
    rep(i,0,5){
        rep(j,0,5){
            cin>>c[i][j];
            sum+=c[i][j];
            par[i][j]={-1,-1};
            used[i][j]=false;
        }
    }
    ans=sum;
    rep(i,0,5){
        rep(j,0,5){
            
            rep(k,0,5){
                rep(l,0,5){
                    par[k][l]={-1,-1};
                    used[k][l]=false;
                }
            }
            if(i==0&&j==0)
            dfs(i,j,c[i][j]);
        }
    }
    cout<<ans<<endl;
}

0