To solve this problem, we need to place two bishops on an n×n chessboard such that their attack regions do not overlap, maximizing the sum of the values of the cells they attack. Each bishop attacks along both of its diagonals. The goal is to find the optimal placement for these bishops.
Approach
-
Precompute Diagonals:
- Calculate the sum of values for each main diagonal (difference between row and column indices,
d = x - y).
- Calculate the sum of values for each anti-diagonal (sum of row and column indices,
e = x + y).
-
Compute Cell Values:
- For each cell, compute the total value influenced by placing a bishop there, which is the sum of its main diagonal and anti-diagonal values minus the cell's own value (since it is counted twice).
-
Sort Cells:
- Sort all cells by their computed value in descending order to prioritize high-value cells.
-
Check Compatibility:
- Evaluate all pairs of top cells (up to a chosen limit) to ensure their placements do not cause overlapping attack regions. This involves checking that their diagonals do not intersect within the board's bounds.
Solution Code
import sys
def main():
n = int(sys.stdin.readline())
grid = []
for _ in range(n):
row = list(map(int, sys.stdin.readline().split()))
grid.append(row)
# Precompute Sm (main diagonal) and Sa (anti-diagonal) sums
max_d = n - 1
Sm = [0] * (2 * max_d + 1) # covers from -(n-1) to +(n-1)
Sa_max_e = 2 * n # since e can go up to (n + n)
Sa = [0] * (Sa_max_e + 2) # 0 ... (2n +1) but we'll compute up to 2n
for x in range(n):
for y in range(n):
cell_val = grid[x][y]
dx = x - y # This is main diagonal difference in 0-based
e = (x + 1) + (y + 1) # e is the 1-based problem coordinates sum: x+1 and y+1, so e_total = (x+1)+(y+1)
Sm[dx + max_d] += cell_val
Sa[e] += cell_val
# Prepare list of cells with their value, coordinates and diagonals' parameters (d, e)
cells = []
for x in range(n):
for y in range(n):
dx = x - y
e_val = (x+1) + (y+1)
current_val = Sm[dx + max_d] + Sa[e_val] - grid[x][y]
cells.append( ( -current_val, x, y, dx, e_val ) )
# Sort by maximum value (since we stored negative for descending sort)
cells.sort()
# Extract the top cells (we take top 300 cells; adjust if needed)
top_K = 300
top_cells = []
for c in cells[:top_K]:
neg_val, x, y, dx, e_val = c
top_cells.append( ( -neg_val, x, y, dx, e_val ) )
max_sum = -1
best_pair = (0,0,0,0)
def is_compat(cellA, cellB):
a_val, ax, ay, ad, ae = cellA
b_val, bx, by, bd, be = cellB
if ad == bd or ae == be:
return False
# Check first scenario: cellA's d and cellB's e
cross1_sum = ad + be
if cross1_sum % 2 ==0:
a_mid = cross1_sum //2
b_mid = (be - ad) //2
if 1<=a_mid <=n and 1<= b_mid <=n:
return False
# Second scenario: cellB's d and cellA's e
cross2_sum = bd + ae
if cross2_sum % 2 ==0:
a_mid2 = cross2_sum //2
b_mid2 = (ae - bd) //2
if 1<=a_mid2 <=n and 1<= b_mid2 <=n:
return False
return True
# Iterate through all pairs in top cells
for i in range(len(top_cells)):
a_val, ax, ay, ad, ae = top_cells[i]
for j in range(i+1, len(top_cells)):
b_val, bx, by, bd, be = top_cells[j]
if is_compat( (a_val, ax, ay, ad, ae), (b_val, bx, by, bd, be) ):
current_sum = a_val + b_val
if current_sum > max_sum:
max_sum = current_sum
best_pair = (ax+1, ay+1, bx+1, by+1)
# If no pair found (unlikely), handle, but problem says there must be at least two cells
print(max_sum)
print(best_pair[0], best_pair[1], best_pair[2], best_pair[3])
if __name__ == "__main__":
main()
Explanation
-
Precompute Diagonals: Two arrays Sm and Sa are used to store the sums of values for each main diagonal (by difference d) and anti-diagonal (by sum e).
-
Cell Value Calculation: For each cell, the potential value it contributes if a bishop is placed there is calculated as the sum of its main and anti-diagonal values minus its own value to avoid double-counting.
-
Sorting Cells: Cells are sorted in descending order of their potential contribution, allowing us to prioritize cells that can contribute the most to the total.
-
Compatibility Check: The top cells are checked pairwise to ensure their placements do not lead to overlapping attack regions. The check involves verifying that their diagonals do not intersect within the chessboard bounds.
By focusing on the top candidates and efficiently checking compatibility, this approach ensures an optimal solution within feasible computational limits.