結果

問題 No.861 ケーキカット
ユーザー Iván SotoIván Soto
提出日時 2021-05-28 08:13:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,269 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 201,324 KB
実行使用メモリ 134,400 KB
最終ジャッジ日時 2024-04-24 15:21:03
合計ジャッジ時間 19,144 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 670 ms
134,272 KB
testcase_01 AC 672 ms
134,400 KB
testcase_02 AC 655 ms
134,272 KB
testcase_03 AC 672 ms
134,400 KB
testcase_04 AC 654 ms
134,400 KB
testcase_05 AC 652 ms
134,272 KB
testcase_06 AC 655 ms
134,272 KB
testcase_07 AC 654 ms
134,400 KB
testcase_08 AC 638 ms
134,272 KB
testcase_09 AC 664 ms
134,272 KB
testcase_10 AC 732 ms
134,400 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *  author: ivanzuki   
 *  created: Thu May 27 2021
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  const int n = 5;
  vector<vector<int>> c(n, vector<int>(n));
  long long total = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> c[i][j];
      total += c[i][j];
    }
  }
  long long ans = (long long) 1e18;
  const vector<int> dx = {-1, 0, 1, 0};
  const vector<int> dy = {0, -1, 0, 1};
  vector<int> was(1 << (n * n));
  function<bool(int, int, int)> On = [&](int mask, int x, int y) {
    return (mask >> (x * n + y)) & 1;
  };
  function<void(int, long long)> DFS = [&](int mask, long long sum) {
    was[mask] = 1;
    ans = min(ans, abs(sum - (total - sum)));
    for (int x = 0; x < n; x++) {
      for (int y = 0; y < n; y++) {
        if (On(mask, x, y)) {
          continue;
        }
        for (int k = 0; k < 4; k++) {
          int xk = x + dx[k];
          int yk = y + dy[k];
          if (0 <= xk && xk < n && 0 <= yk && yk < n && On(mask, xk, yk) && !was[mask | (1 << (x * n + y))]) {
            DFS(mask | (1 << (x * n + y)), sum + c[x][y]);
          }
        }
      }
    }
  };
  DFS(1, c[0][0]);
  cout << ans << '\n';
  return 0;
}
0