結果

問題 No.861 ケーキカット
ユーザー i_mrhji_mrhj
提出日時 2019-09-05 18:11:11
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,263 bytes
コンパイル時間 761 ms
コンパイル使用メモリ 30,696 KB
実行使用メモリ 5,228 KB
最終ジャッジ日時 2023-09-02 15:32:38
合計ジャッジ時間 2,219 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
4,504 KB
testcase_01 AC 11 ms
4,384 KB
testcase_02 AC 8 ms
4,376 KB
testcase_03 AC 15 ms
4,380 KB
testcase_04 AC 13 ms
4,376 KB
testcase_05 AC 17 ms
4,376 KB
testcase_06 AC 52 ms
4,376 KB
testcase_07 AC 9 ms
4,384 KB
testcase_08 AC 23 ms
4,380 KB
testcase_09 WA -
testcase_10 AC 9 ms
4,376 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 #

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) > (b) ? (b) : (a))
#define abs(x) ((x) > 0 ? (x) : -(x))
#define MOD (ll)1000000007 //10^9 + 7
#define endl printf("\n")
typedef long long ll;
#define MAX_N (1 << 17)

int d[5][5]; ll c[5][5];
ll sum = 0, ans = LLONG_MAX;

void
dfs(int i, ll r, bool sign)
{
  if (i == 25  || r >= sum / (ll)2 + (ll)1) {
    ans = Min(ans, abs(sum - ((ll)2) * r));
    return;
  }
  int s = i / 5, t = i % 5;
  if (!sign) {
    d[s][t] = 1;
    dfs(i + 1, r + c[s][t], true);
    d[s][t] = 0;
    dfs(i + 1, r, false);
  } else {
    for (int x = -1; x <= 1; x++) {
      for (int y = -1; y <= 1; y++) {
	if (x * y == 0 &&
	    0 <= s + x && s + x < 5 &&
	    0 <= t + y && t + y < 5 && 
	    d[s + x][t + y] == 1) {
	  d[s][t] = 1;
	  dfs(i + 1, r + c[s][t], true);
	  d[s][t] = 0;
	  break;
	}
      }
    }
    dfs(i + 1, r, true);
  }
  return;
}
    
	  
  
int
main(int argc, char *argv[])
{
  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      scanf("%lld", &c[i][j]);
      sum += c[i][j];
    }
  }

  dfs(0, 0, false);

  printf("%lld\n", ans);

  return 0;
}

  

  
0