結果

問題 No.861 ケーキカット
ユーザー H3PO4H3PO4
提出日時 2022-08-20 12:50:55
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,112 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 82,052 KB
実行使用メモリ 203,556 KB
最終ジャッジ日時 2024-04-17 11:51:32
合計ジャッジ時間 4,807 ms
ジャッジサーバーID
(参考情報)
judge2 / 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 deque

d = deque([1])
cakes = set()
SIZE = 5
C = tuple(tuple(map(int, input().split())) for _ in range(SIZE))
S = sum(sum(x for x in row) for row in C)


def is_connected(v, x, y):
    for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        xdx = x + dx
        ydy = y + dy
        if not (0 <= xdx < SIZE and 0 <= ydy < SIZE):
            continue
        neighbor_idx = ydy * SIZE + xdx
        if (v >> neighbor_idx) & 1:
            return True
    return False


while d:
    v = d.pop()
    cakes.add(v)
    for x in range(SIZE):
        for y in range(SIZE):
            idx = y * SIZE + x
            if (v >> idx) & 1:
                continue
            w = v | (1 << idx)
            if w in cakes:
                continue
            if is_connected(v, x, y):
                d.append(w)

ans = 10 ** 18
for c in cakes:
    tmp = 0
    for x in range(SIZE):
        for y in range(SIZE):
            idx = y * SIZE + x
            if (c >> idx) & 1:
                tmp += C[x][y]
    tmp_ans = abs(tmp - (S - tmp))
    if ans > tmp_ans:
        ans = tmp_ans
print(ans)
0