Next, we explain how to represent the coloring of the board using binary variables.
Representation of Boards Satisfying Row Hints¶
We represent a board that satisfies the row hints by assigning numbers to certain squares, as shown
in
the diagram below.
In each row, numbers $0, 1, \ldots$ are written from left to right on the leftmost square of each
consecutive block of black squares.
When performing this operation, the following rules must be satisfied for each row.
If the hints for a row are $h_0, h_1, \ldots, h_{n-1}$:
- The numbers $0$ to $n-1$ must each appear exactly once in that row.
- For $0 \leq k < n-1$, the square with the number $k+1$ must be at least $h_k + 1$ squares to
the
right of the square with the number $k$.
- The square with the number $n-1$ cannot be to the right of the $h_{n-1}$-th square from the right
edge.
For example, if the hints for a row are 2 1 2, these properties are illustrated below.
Setting Up Row Variables¶
We use binary variables to represent the numbers written in each square. For each square, we prepare
several binary variables equal to the number of hints for that row. If the $i$-th variable is 1, it
means
the number $i$ is written in that square. For example, in the second row of the figure above, there
are
two hints. The number 0 is in the second square from the left, and the number 1 is in the fourth
square.
This can be represented using a $5 \times 2$ table of binary variables:
| Square \ Number |
Number 0 |
Number 1 |
| 1st square from left |
0 |
0 |
| 2nd square from left |
1 |
0 |
| 3rd square from left |
0 |
0 |
| 4th square from left |
0 |
1 |
| 5th square from left |
0 |
0 |
In this table, both variables for the first square are 0, which means no number is written in this
square. For the second square, the variable representing "Number 0" is 1, which means zero is written
there.
In this way, we set up (number of columns) $\times$ (number of hints for that row) binary variables
for
each row. We'll use $q^\text{row}_{i,j,k}$ for the binary variable that represents whether number $k$
is
written in the square at row $i$, column $j$.
Constraints¶
Next, we need to add constraints to $q^\text{row}_{i,j,k}$ to ensure it correctly represents a board
that
satisfies the row hints.
First, a square can have at most one number written on it, or none at all; it cannot have two. This
means
that for each square, at most one of its corresponding binary variables (one for each hint) can be 1.
In
equation form:
$$
\sum_k q^\text{row}_{i,j,k} \leq 1
$$
Next, we add conditions for each row to ensure it satisfies its hints.
Assume the hints for a row are $h_0, h_1, \ldots, h_{n-1}$. As mentioned earlier, the following three
rules must hold:
- The numbers $0$ to $n-1$ must each appear exactly once in that row.
- For $0 \leq k < n-1$, the square with the number $k+1$ must be at least $h_k + 1$ squares to
the
right of the square with the number $k$.
- The square with the number $n-1$ cannot be to the right of the $h_{n-1}$-th square from the right
edge.
First, "The numbers $0$ to $n-1$ must each appear exactly once in that row." In our binary variable
table, this means the sum of each "Number" column must be 1. For each $k$ from $0$ to $n-1$, only one
variable representing $k$ being written in that row should be 1. In equation form:
$$
\sum_j q^\text{row}_{i,j,k} = 1
$$
Second, "For $0 \leq k < n-1$, the square with number $k+1$ must be at least $h_k + 1$ squares to
the
right of the square with number $k$." This is a constraint for all but the last hint number. We can
rephrase this as: "We cannot have number $k$ in column $j_1$ AND number $k+1$ in column $j_2$ if $j_2
-
j_1 < h_k+1$." In equation form:
$$
q^\text{row}_{i,j_1,k} q^\text{row}_{i, j_2, k+1} = 0 \quad (j_2 - j_1 < h_k + 1,\ \text{$h_k$ is
the
$k$-th hint for row $i$})
$$
Finally, "The square with number $n-1$ cannot be to the right of the $h_{n-1}$-th square from the
right
edge." This is a constraint for the last hint number in each row. In equation form:
$$
q^\text{row}_{i, j, n-1} = 0 \quad (j > (\text{number of columns}) - h_{n-1})
$$
Conversely, if $q^\text{row}$ satisfies all these conditions, it will be a valid representation of a
board satisfying the row hints.
Representation of Boards Satisfying Column
Hints¶
We can apply the same approach for the column hints. We define binary variables $q^\text{col}$, this
time
setting up (number of rows) $\times$ (number of hints) variables for each column.
A board satisfying the column hints would look like this for the example given earlier:
We use $q^\text{col}_{j, i, k}$ for the variable that represents the number $k$ being written in the
square at row $i$, column $j$. Note that the column index $j$ comes first.
We can add the same types of constraints for $q^\text{col}$ to ensure it represents a valid board for
the
column hints. Simply refer to the previous section, swapping "row" for "column" and "left" for "top."
Constraint for Matching Boards¶
Now we need to add constraints to ensure that the two sets of binary variables, $q^\text{row}$ and
$q^\text{col}$, describe the same colored board.
First, let's consider how to reconstruct the board (which squares are colored) from the
$q^\text{row}$
values.
For example, if a row's hints are 2 1 2, a square in that row is colored black if it
meets
any of the following conditions:
- Number 0 is written in that square or one square to its left.
- Number 1 is written in that square.
- Number 2 is written in that square or one square to its left.
Because of the constraints we have already set on $q^\text{row}$, these conditions cannot overlap.
Therefore, if the hint for row $i$ is 2 1 2, the square at row $i$, column $j$ is black
if
the following expression is 1, and white if it is 0.
$$
q^\text{row}_{i, j, 0} + q^\text{row}_{i, j-1, 0} + q^\text{row}_{i, j, 1} + q^\text{row}_{i, j, 0} +
q^\text{row}_{i, j-1, 0}
$$
Even with different hint numbers, we can find a similar linear expression of $q^\text{row}$ for each
square's color. In general, if the hints for row $i$ are $h_0, h_1, \ldots$, the color of the square
at
row $i$, column $j$ ($C^\text{row}_{i,j}$) is black if the value is 1, and white if 0.
$$
C^\text{row}_{i,j} = \displaystyle\sum_k \sum_{r=j-h_k+1}^j q^\text{row}_{i,r,k}
$$
We can do the same for $q^\text{col}$. If the hints for column $j$ are $H_0, H_1, \ldots$, the color
of
the square at row $i$, column $j$ is:
$$
C^\text{col}_{i,j} = \displaystyle\sum_k \sum_{r=i-H_k+1}^j q^\text{col}_{j,r,k}
$$
Therefore, to make the boards match, we simply need to add a constraint that $C^\text{row}{i,j} =
C^\text{col}_{i,j}$ for every square $(i, j)$.
$$
\sum_k \sum_{r=j-h_k+1}^j q^\text{row}_{i,r,k} = \sum_k \sum_{r=i-H_k+1}^j q^\text{col}_{j,r,k}
$$
This constraint ensures that the board reconstructed from $q^\text{row}$ and the board from
$q^\text{col}$ are the same.
With this, the Picross formulation is complete.