内容中心

2026年质量好的蔬菜泡沫包装箱品牌厂家推荐-昆明官宝泡沫有限公司

To solve this problem, we need to determine the minimum number of cell flips required to make all rows and columns of a 4x4 grid have even sums.

Approach

  1. Problem Analysis: For a 4x4 grid, the sum of all row sums equals the sum of all column sums. Thus, if all rows have even sums, the sum of column sums must also be even. This property allows us to leverage a systematic approach to find the minimal flips.
  2. Key Insight: The first 3x3 subgrid can be used to determine the rest of the grid. For each possible combination of the first 3x3 cells (flip or not), we can compute the required flips for the 4th column (to make rows even) and the 4th row (to make columns even). The 4th cell of the 4th row is determined by both the 4th row and column constraints, which are consistent.
  3. Algorithm:
    • Compute the parity (even/odd sum) of each row and column in the original grid.
    • Iterate over all possible 3x3 binary matrices (each cell 0 or 1, representing no flip or flip).
    • For each combination, compute the necessary flips for the 4th column and row.
    • Calculate the total number of flips and track the minimum.

Solution Code

# Read the 4x4 grid
grid = [list(map(int, input().split())) for _ in range(4)]

# Compute row parities (0 for even, 1 for odd)
row_parity = [sum(row) % 2 for row in grid]

# Compute column parities (0 for even, 1 for odd)
col_parity = [sum(col) % 2 for col in zip(*grid)]

min_flips = float('inf')

# Iterate all possible 3x3 flip combinations (9 bits, 0 to 511)
for mask in range(0, 1 << 9):
    flip = [[0] * 4 for _ in range(4)]

    # Fill the first 3x3 grid from the mask
    idx = 0
    for i in range(3):
        for j in range(3):
            flip[i][j] = (mask >> idx) & 1
            idx += 1

    # Fill the 4th column (for rows 0-2) to make row sums even
    for i in range(3):
        sum_row = sum(flip[i][0:3])
        flip[i][3] = (row_parity[i] - sum_row) % 2

    # Fill the 4th row (for columns 0-2) to make column sums even
    for j in range(3):
        sum_col = sum(flip[x][j] for x in range(3))
        flip[3][j] = (col_parity[j] - sum_col) % 2

    # Compute the 4th cell of the 4th row (must satisfy both row and column parity)
    sum_row3 = sum(flip[3][0:3])
    flip33_row = (row_parity[3] - sum_row3) % 2

    sum_col3 = sum(flip[x][3] for x in range(3))
    flip33_col = (col_parity[3] - sum_col3) % 2

    # These two values are always equal due to parity consistency
    flip[3][3] = flip33_row

    # Calculate total flips for this combination
    total = sum(sum(row) for row in flip)

    # Update the minimal flips
    if total < min_flips:
        min_flips = total

print(min_flips)

Explanation

  • Row and Column Parity: We first compute the parity of each row and column to know how many flips are needed to make them even.
  • 3x3 Combinations: By iterating over all possible 3x3 flip combinations (512 total), we cover all possible ways to adjust the grid.
  • Determine Remaining Cells: For each combination, we adjust the 4th column and row to ensure even sums, then compute the final cell.
  • Minimal Flips: The minimal number of flips across all combinations gives the solution.

This approach efficiently finds the minimal flips using brute force on the 3x3 subgrid, which is feasible due to its small size. The result is the optimal solution for the problem.

昆明官宝泡沫有限公司

昆明官宝泡沫有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?