Consider a table of cells, which are colored by four colors Azure, Coral, Green, and Taupe. The only condition for it to be nice is that every subtable of has 4 distinct colors. (1)

Given a colored table, we need to re-color it so that it satisfies the condition above. What is the minimum number of cells that need to be re-colored? Read the problem statement.

We need to prove the following theorem:

Theorem. Such a table satisfying (1) has:

  • every row has two alternate colors, or
  • every column has two alternate colors

Proof

It is obvious that every row or column cannot have only one colors. We will prove that if a column of the nice table contains at least 3 different colors, then every row contains only 2 colors.

Suppose that column has at least 3 different colors. We can find such that the colors of (i-th row, j-th column), , and are pairwise different. Consider a column that is two columns away from . Without loss of generosity, let it be the column . Since is a nice table, forms a full set of colors ACGT, and so is .

That implies . Similarly we can show that . The two equalities give us and .

Columns are repeated

Until now, we’ve discovered that if there are three consecutive cells with different colors on column , then the corresponding cells on column will have the same color in that order. Using induction we can prove that the corresponding cells on any column will have the same color in that order.

Furthermore, there is only one way to color three cells between column and . This implies that we can figure out the color of the whole row , , and .

All cells on the rows can be deduced, and in each row the colors are alternating

We observe that in a nice table, if one row has two alternating colors, say A and C, then the nearby row also has two alternating colors (G and T), and all other rows also have two alternating colors (A-C or G-T). (QED)

A similar proof can be done to show the rest of the theorem.

Algorithm

Let be the nice table deduced from the given table through the minimum number of recoloring steps.

We shall check two cases: has rows of alternating colors, or has columns of alternating colors. Whichever has smaller amount of differences from the given table will be chosen.

If has rows of alternating colors, we select two colors for the first row, say X and Y, then the other two colors (say Z and W) will present in the second row, and the third row will have X and Y again, and so on.

But should the row begin with “XYXY…” or “YXYX…”? Greedily speaking, we should choose the pattern that minimize the number of differences, because our decision of which pattern to use on this row doesn’t affect other rows. We try all cases of X and Y, then continue with the case of columns of alternating colors, to select the best table.

See my solution in C++.