結果
問題 |
No.861 ケーキカット
|
ユーザー |
|
提出日時 | 2019-08-09 22:55:31 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,027 bytes |
コンパイル時間 | 741 ms |
コンパイル使用メモリ | 64,920 KB |
実行使用メモリ | 18,944 KB |
最終ジャッジ日時 | 2024-07-19 15:18:15 |
合計ジャッジ時間 | 7,048 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 8 WA * 13 |
ソースコード
#include<iostream> 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; if(val == (1<<25)-1) return; 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)]) 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; 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; }