結果

問題 No.861 ケーキカット
ユーザー qibqib
提出日時 2023-04-19 02:03:55
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 854 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 192,640 KB
最終ジャッジ日時 2024-04-22 03:11:21
合計ジャッジ時間 5,188 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

from collections import defaultdict

c = [list(map(int, input().split())) for _ in range(5)]

s = sum([c[i][j] for i in range(5) for j in range(5)])

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

ans = abs(s - c[0][0] - c[0][0])
dq = []
dq.append((1, c[0][0]))
used = defaultdict(bool)
used[1] = True
while len(dq) > 0:
  mask, score = dq.pop()
  q = []
  for i in range(25):
    if (mask >> i) & 1 != 0:
      cx, cy = i // 5, i % 5
      for i in range(4):
        nx = cx + dx[i]
        if nx < 0 or nx >= 5:
          continue
        ny = cy + dy[i]
        if ny < 0 or ny >= 5:
          continue
        nxt = mask | (1 << (nx * 5 + ny))
        if (mask >> (nx * 5 + ny)) & 1 != 0 or used[nxt]:
          continue
        used[nxt] = True
        ans = min(ans, abs(s - 2 * (score + c[nx][ny])))
        dq.append((nxt, score + c[nx][ny]))

print(ans)
0