1. Introduction#
Troix is a logic puzzle described as follows: given a grid of squares
with some cells already filled with X X X , O O O , or I I I . Player needs to complete
the grid by filling in the empty cells with X X X , O O O , and I I I , satisfying the
following constraints:
The grid is completely covered by X X X , O O O and I I I
In each row/column, there is no sequence of more than 2 consecutive cells filled with the same symbol.
In each row/column, the number of X X X , O O O , and I I I is equal.
Below is an example of a 12×12 Troix puzzle and its solution.
2. Modeling puzzle#
n R n_R n R is the number of rows in the grid (n R n_R n R is a multiple of 3)
n C n_C n C is the number of columns in the grid (n C n_C n C is a multiple of 3)
F X F_X F X is the set of all cells ( i , j ) (i, j) ( i , j ) already filled with X X X
F O F_O F O is the set of all cells ( i , j ) (i, j) ( i , j ) already filled with O O O
F I F_I F I is the set of all cells ( i , j ) (i, j) ( i , j ) already filled with I I I
2.1. Decision variables#
For each cell ( i , j ) (i, j) ( i , j ) in the grid, we define variables x ( i , j ) x(i, j) x ( i , j ) , y ( i , j ) y(i, j) y ( i , j ) , z ( i , j ) z(i, j) z ( i , j ) such that
x ( i , j ) = { 1 if cell ( i , j ) filled with X 0 otherwise x(i, j) = \begin{cases}
1 & \text{if cell } (i, j) \text{ filled with } X \\
0 & \text{otherwise }
\end{cases}
x ( i , j ) = { 1 0 if cell ( i , j ) filled with X otherwise
y ( i , j ) = { 1 if cell ( i , j ) filled with O 0 otherwise y(i, j) = \begin{cases}
1 & \text{if cell } (i, j) \text{ filled with } O\\
0 & \text{otherwise}
\end{cases}
y ( i , j ) = { 1 0 if cell ( i , j ) filled with O otherwise
z ( i , j ) = { 1 if cell ( i , j ) filled with I 0 otherwise z(i, j) = \begin{cases}
1 & \text{if cell } (i, j) \text{ filled with } I\\
0 & \text{otherwise}
\end{cases}
z ( i , j ) = { 1 0 if cell ( i , j ) filled with I otherwise
2.2. Constraints#
{ x ( i , j ) = 1 , ∀ ( i , j ) ∈ F X y ( i , j ) = 1 , ∀ ( i , j ) ∈ F O z ( i , j ) = 1 , ∀ ( i , j ) ∈ F I \begin{cases}
x(i, j) = 1, \forall (i, j) \in F_X \\
y(i, j) = 1, \forall (i, j) \in F_O \\
z(i, j) = 1, \forall (i, j) \in F_I
\end{cases}
⎩ ⎨ ⎧ x ( i , j ) = 1 , ∀ ( i , j ) ∈ F X y ( i , j ) = 1 , ∀ ( i , j ) ∈ F O z ( i , j ) = 1 , ∀ ( i , j ) ∈ F I
The grid is completely covered by X X X , O O O and I I I
x ( i , j ) + y ( i , j ) + z ( i , j ) = 1 , ∀ 0 ≤ i < n R , 0 ≤ j < n C x(i, j) + y(i, j) + z(i, j) = 1, \\ \forall 0 \leq i < n_R, 0 \leq j < n_C
x ( i , j ) + y ( i , j ) + z ( i , j ) = 1 , ∀0 ≤ i < n R , 0 ≤ j < n C
In each row, there is no sequence of more than 2 consecutive cells filled with the same symbol.
{ x ( i , j ) + x ( i , j + 1 ) + x ( i , j + 2 ) ≤ 2 y ( i , j ) + y ( i , j + 1 ) + y ( i , j + 2 ) ≤ 2 z ( i , j ) + z ( i , j + 1 ) + z ( i , j + 2 ) ≤ 2 ∀ 0 ≤ i < n R , 0 ≤ j < n C − 2 \begin{cases}
x(i, j) + x(i, j + 1) + x(i, j + 2) \leq 2 \\
y(i, j) + y(i, j + 1) + y(i, j + 2) \leq 2 \\
z(i, j) + z(i, j + 1) + z(i, j + 2) \leq 2 \\
\end{cases}
\\ \forall 0 \leq i < n_R, 0 \leq j < n_C - 2
⎩ ⎨ ⎧ x ( i , j ) + x ( i , j + 1 ) + x ( i , j + 2 ) ≤ 2 y ( i , j ) + y ( i , j + 1 ) + y ( i , j + 2 ) ≤ 2 z ( i , j ) + z ( i , j + 1 ) + z ( i , j + 2 ) ≤ 2 ∀0 ≤ i < n R , 0 ≤ j < n C − 2
In each column, there is no sequence of more than 2 consecutive cells filled with the same symbol.
{ x ( i , j ) + x ( i + 1 , j ) + x ( i + 2 , j ) ≤ 2 y ( i , j ) + y ( i + 1 , j ) + y ( i + 2 , j ) ≤ 2 z ( i , j ) + z ( i + 1 , j ) + z ( i + 2 , j ) ≤ 2 ∀ 0 ≤ i < n R − 2 , 0 ≤ j < n C \begin{cases}
x(i, j) + x(i + 1, j) + x(i + 2, j) \leq 2 \\
y(i, j) + y(i + 1, j) + y(i + 2, j) \leq 2 \\
z(i, j) + z(i + 1, j) + z(i + 2, j) \leq 2 \\
\end{cases}
\\ \forall 0 \leq i < n_R - 2, 0 \leq j < n_C
⎩ ⎨ ⎧ x ( i , j ) + x ( i + 1 , j ) + x ( i + 2 , j ) ≤ 2 y ( i , j ) + y ( i + 1 , j ) + y ( i + 2 , j ) ≤ 2 z ( i , j ) + z ( i + 1 , j ) + z ( i + 2 , j ) ≤ 2 ∀0 ≤ i < n R − 2 , 0 ≤ j < n C
In each row, the number of X X X , O O O , and I I I is equal.
{ ∑ j = 0 n C − 1 x ( i , j ) = n C 3 ∑ j = 0 n C − 1 y ( i , j ) = n C 3 ∑ j = 0 n C − 1 z ( i , j ) = n C 3 ∀ 0 ≤ i < n R \begin{cases}
\sum\limits_{j = 0}^{n_C - 1}{x(i, j)} = {n_C \over 3} \\
\sum\limits_{j = 0}^{n_C - 1}{y(i, j)} = {n_C \over 3} \\
\sum\limits_{j = 0}^{n_C - 1}{z(i, j)} = {n_C \over 3} \\
\end{cases}
\\ \forall 0 \leq i < n_R
⎩ ⎨ ⎧ j = 0 ∑ n C − 1 x ( i , j ) = 3 n C j = 0 ∑ n C − 1 y ( i , j ) = 3 n C j = 0 ∑ n C − 1 z ( i , j ) = 3 n C ∀0 ≤ i < n R
In each column, the number of X X X , O O O , and I I I is equal.
{ ∑ i = 0 n R − 1 x ( i , j ) = n R 3 ∑ i = 0 n R − 1 y ( i , j ) = n R 3 ∑ i = 0 n R − 1 z ( i , j ) = n R 3 ∀ 0 ≤ j < n C \begin{cases}
\sum\limits_{i = 0}^{n_R - 1}{x(i, j)} = {n_R \over 3} \\
\sum\limits_{i = 0}^{n_R - 1}{y(i, j)} = {n_R \over 3} \\
\sum\limits_{i = 0}^{n_R - 1}{z(i, j)} = {n_R \over 3} \\
\end{cases}
\\ \forall 0 \leq j < n_C
⎩ ⎨ ⎧ i = 0 ∑ n R − 1 x ( i , j ) = 3 n R i = 0 ∑ n R − 1 y ( i , j ) = 3 n R i = 0 ∑ n R − 1 z ( i , j ) = 3 n R ∀0 ≤ j < n C
2.3. Objective function#
This problem does not have an objective function, as we are not aiming to minimize
or maximize any value. Our goal is simply to find a feasible solution. Technically,
when programming, we can set the objective function to a constant (I often choose 0)
3. Conclusion#
For more Troix puzzles and variations, please refer to Krazydad .
The Python code for Troix modeling is available at
Tung-hehe
Happy modeling!