7

Some squares of a $n \times n$ table ($n>2$) are black, the rest are white. In every white square, we write the number of the black squares having at least one vertex with it. Find the max possible sum of these numbers.

What I did until now was just trying for small n and did not catch anything generalizable.

RobPratt
  • 50,938
  • So what did you get for $n=2,3,4$? – almagest Jan 11 '20 at 16:41
  • 1
    I get 12 for $n=3$, obtained by coloring the four corner squares black. How did you get 14? – RobPratt Jan 11 '20 at 16:59
  • 1
    @PedrotenZo: By alternating rows or columns of one colour. I believe this is also the general solution. – joriki Jan 11 '20 at 17:02
  • 3
    Please try to make the titles of your questions more informative. For example, Why does $a<b$ imply $a+c<b+c$? is much more useful for other users than A question about inequality. From How can I ask a good question?: Make your title as descriptive as possible. In many cases one can actually phrase the title as the question, at least in such a way so as to be comprehensible to an expert reader. You can find more tips for choosing a good title here. – Shaun Jan 11 '20 at 17:20
  • OK, I see now. I confirm that alternating monochromatic lines is optimal up to $n=10$. – RobPratt Jan 11 '20 at 17:31
  • 2
    @OP you been posting questions and then deleting them, e.g. here. Also, in the deleted question above, you said it was from a contest (IZHO), and the contest is ongoing. At the very minimum, please tell us where this question is from, and, promise not to delete it. – antkam Jan 12 '20 at 00:39
  • Thanks for accepting my proof using the vertex count idea of Joriki. I've now added a proof along the lines of another, although related, idea I had worked on. So long as it's understandable to everyone I think it's probably the nicer proof. –  Jan 12 '20 at 02:48
  • 1
    This is the maximum cut problem in the corresponding graph. – RobPratt Jan 12 '20 at 20:06

4 Answers4

6

The asymmetric formulation somewhat conceals the symmetry of the problem. We're counting pairs of white and black squares that share at least one vertex.

Consider each internal vertex and the set of four squares adjacent to it. Two of them that share an edge are also together in the square set of another vertex, whereas those that are diagonally opposed are only together in the square set of this vertex. Thus, award half a point to the vertex for each pair of opposite-coloured squares that share an edge, and one point for each pair of opposite-coloured squares that are diagonally opposed. Then summing over all vertices will reproduce the desired score (if we treat the margins properly).

There are only four inequivalent configurations for the square set of a vertex:

  • All squares are the same colour: score $0$.
  • One square is a different colour than the other three: score $2$.
  • Two squares of each colour are diagonally opposed: score $2$.
  • Two squares of each colour share an edge: score $3$.

Thus, we should expect the optimal configuration to ideally have score $3$ at all internal vertices, with two squares of each colour sharing an edge. This is realized by alternating monochromatic rows or columns.

To turn this into a rigorous proof, you'll have to deal with the margins, but the fact that we can maximize the score in the bulk strongly suggests that the alternating row or column pattern should be optimal, at least for large $n$ where the bulk dominates the margins.

The total count from this solution is

$$ \frac{8+(3+3+4+4)(n-2)+6(n-2)^2}2=3n^2-5n+2\;. $$

This is OEIS sequence A049451 with an offset of $1$. The entry states that this is “With offset 1: the maximum possible sum of numbers in an $N\times N$ standard Minesweeper grid”, which is precisely your problem. Unfortunately, no reference to a proof is given.

joriki
  • 242,601
  • Discussed for $m \times n$ board here. – RobPratt Jan 11 '20 at 18:05
  • 1
    I have (I hope) now given a rigorous proof. Although your vertex idea was not strictly necessary because I was only dealing with a strip of squares, it was invaluable in enabling me to write out the proof in a comprehensible way - which I was finding difficult until I read your clearly explained proof. –  Jan 11 '20 at 23:06
1

An elementary proof

Consider adding an extra row and column to an $(n-1)\times(n-1)$ board. The total count will be increased by an amount equal to the number of extra pairs of opposite coloured squares which have a vertex in common.

The bold edges shown on the image represent all these possible pairs in the case $n=4$. The edges have been grouped into arrowheads of four edges and one group of $6$ edges.

The possible extra pairs

In general there will be $2n-4$ arrowheads. Three of the edges of each arrowhead form a triangle and therefore all three cannot link oppositely coloured squares. Therefore each arrowhead contributes at most $3$ to the extra count. Similarly the group of $6$ edges contributes at most $4$.

An upper bound on the additional score is therefore $$3(2n-4)+4=6n-8$$

For a $1\times 1$ board the score is $0$ and so an upper bound for the $n\times n$ board is $$\sum_{i=2}^n (6i-8)=3n^2-5n+2.$$

This completes the proof since this score is attained by a striped board.

RobPratt
  • 50,938
1

For fixed $n$, you can solve the problem via integer linear programming as follows. Define a graph $G=(N,E)$ with a node for each square and a link for each pair of adjacent squares. Let binary decision variable $x_i$ indicate whether node $i\in N$ is black. We want to maximize $\sum_{(i,j)\in E} (x_i (1-x_j)+x_j (1-x_i))$. To linearize this quadratic objective, introduce binary variable $y_{i,j}$ to represent the summand. The problem is to maximize $\sum_{(i,j)\in E} y_{i,j}$ subject to linear constraints $$y_{i,j} \le x_i + x_j \le 2 - y_{i,j}.$$ If $y_{i,j} = 1$, the constraints force $x_i + x_j = 1$, so $x_i (1-x_j)+x_j (1-x_i) = 1$, as desired.

By the way, we are solving the maximum cut problem for $G$.

Example: for $n=3$, number the squares from 1 to 9 as follows: \begin{matrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 &8 &9 \end{matrix} The problem is then to maximize $$y_{1,2} + y_{1,4} + y_{1,5} + y_{2,3} + y_{2,4} + y_{2,5} + y_{2,6} + y_{3,5} + y_{3,6} + y_{4,5} + y_{4,7} + y_{4,8} + y_{5,6} + y_{5,7} + y_{5,8} + y_{5,9} + y_{6,8} + y_{6,9} + y_{7,8} + y_{8,9}$$ subject to \begin{align} y_{1,2} &\le x_1 + x_2 \le 2 - y_{1,2}\\ y_{1,4} &\le x_1 + x_4 \le 2 - y_{1,4}\\ y_{1,5} &\le x_1 + x_5 \le 2 - y_{1,5}\\ y_{2,3} &\le x_2 + x_3 \le 2 - y_{2,3}\\ y_{2,4} &\le x_2 + x_4 \le 2 - y_{2,4}\\ y_{2,5} &\le x_2 + x_5 \le 2 - y_{2,5}\\ y_{2,6} &\le x_2 + x_6 \le 2 - y_{2,6}\\ y_{3,5} &\le x_3 + x_5 \le 2 - y_{3,5}\\ y_{3,6} &\le x_3 + x_6 \le 2 - y_{3,6}\\ y_{4,5} &\le x_4 + x_5 \le 2 - y_{4,5}\\ y_{4,7} &\le x_4 + x_7 \le 2 - y_{4,7}\\ y_{4,8} &\le x_4 + x_8 \le 2 - y_{4,8}\\ y_{5,6} &\le x_5 + x_6 \le 2 - y_{5,6}\\ y_{5,7} &\le x_5 + x_7 \le 2 - y_{5,7}\\ y_{5,8} &\le x_5 + x_8 \le 2 - y_{5,8}\\ y_{5,9} &\le x_5 + x_9 \le 2 - y_{5,9}\\ y_{6,8} &\le x_6 + x_8 \le 2 - y_{6,8}\\ y_{6,9} &\le x_6 + x_9 \le 2 - y_{6,9}\\ y_{7,8} &\le x_7 + x_8 \le 2 - y_{7,8}\\ y_{8,9} &\le x_8 + x_9 \le 2 - y_{8,9} \end{align} One optimal solution has $x_4=x_5=x_6=1$, $$y_{1,4}=y_{1,5}=y_{2,4}=y_{2,5}=y_{2,6}=y_{3,5}=y_{3,6}=y_{7,4}=y_{7,5}=y_{8,4}=y_{8,5}=y_{8,6}=y_{9,5}=y_{9,6}=1,$$ and all other variables 0.

RobPratt
  • 50,938
0

A general proof

Consider an inductive step of adding an extra row and column to an $(n-1)\times(n-1)$ board, for $n\ge 2$.

The image shows the three types of vertices we need to deal with, shown in the case $n=4$.

The vertices
In general, there will be two vertices of type A, one of type B and $2(n-2)$ of type C.

We shall award points to these vertices by the following method which is designed so that the sum of these points will be precisely the extra score created by adding the row and column.

Type A The score will be $0$ or $0.5$ depending upon whether the adjacent squares have the same colour or not.

Type B and Type C Award points to pairs of oppositely coloured squares as follows:

1 point if they are diagonally opposite;

1 point if they are both on an outside edge;

0.5 points if they share an edge and only one of the squares is on an outside edge.

Thus a maximum of $3.5$ points can be awarded to the type B vertex and a maximum of $3$ points to a type C vertex.

An upper bound on the additional score is therefore the integer part of $$2\times0.5+3.5+3\times2(n-2)=6n-7.5$$ i.e $6n-8$.

For a $1\times 1$ board the score is $0$ and so an upper bound for the $n\times n$ board is $$\sum_{i=2}^n (6i-8)=3n^2-5n+2.$$

This completes the proof since this score is attained by a striped board.

N.B The scores awarded to points differ from those used by Joriki because we are not counting points already awarded in the $(n-1)\times(n-1)$ board and, for type C, some adjacencies are not counted by any other vertex.

RobPratt
  • 50,938