結果

問題 No.1345 Beautiful BINGO
ユーザー H3PO4
提出日時 2021-01-16 15:07:48
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,329 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 82,116 KB
実行使用メモリ 160,628 KB
最終ジャッジ日時 2024-11-27 17:29:40
合計ジャッジ時間 50,709 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 48 TLE * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
from copy import deepcopy

N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]


def make_table(t, slash, backslash):
    table = deepcopy(A)
    solved = 0
    for y, b in enumerate(t):
        if b:
            for x in range(N):
                solved += table[y][x]
                table[y][x] = 0
    if slash:
        for i in range(N):
            solved += table[i][i]
            table[i][i] = 0
    if backslash:
        for i in range(N):
            solved += table[N - i - 1][i]
            table[N - i - 1][i] = 0

    return table, solved


ans = 100 * N * N

# 横と斜めは全列挙
slash_pair = ((0, 0), (1, 0), (0, 1), (1, 1))
for t in itertools.product((0, 1), repeat=N):
    row = t.count(1)
    if not M - N - 2 <= row <= M:
        continue
    for slash, backslash in slash_pair:
        col = M - row - slash - backslash  # 削るべき列数
        if not 0 <= col <= N:
            continue
        table, solved = make_table(t, slash, backslash)

        additional_solved = [0] * N

        # 縦のうれしい順に選ぶ
        for i in range(N):
            additional_solved[i] = sum(table[j][i] for j in range(N))
        additional_solved.sort()
        solved += sum(additional_solved[:col])
        ans = min(ans, solved)
print(ans)
0