Binary Sudoku (also known as Takuzu or Binairo) is a logic puzzle played on an $n \times n$ grid board, where $n$ is an even number. The goal is to fill all empty cells with only zeros and ones while respecting three fundamental rules:
Balance: Each row and each column must contain exactly the same number of zeros and ones.
Consecutive limit: No more than two identical numbers can be adjacent (neither horizontally nor vertically).
Uniqueness: No two rows and no two columns can be identical.
A Binary Sudoku puzzle starts with some cells already filled with initial values. Below is an example of an $8 \times 8$ board:
Would you like to try solving it logically before looking at its mathematical formulation?
Challenge solution
The solution to the Binary Sudoku presented above is given by:
Binary Sudoku can be formulated as a binary linear optimization model, do you dare to try it?
To explain how to solve Binary Sudoku systematically, we can use a binary linear optimization model. First, we define the elements of the model:
Index sets
\(\mathcal{R} = \{1, \dots, n\}\): Board rows.
\(\mathcal{C} = \{1, \dots, n\}\): Board columns.
\(\mathcal{I} \subset \mathcal{R} \times \mathcal{C}\): Set of positions with a fixed initial value.
Parameters
\(n\): Board dimension (must be an even number).
\(A_{rc} \in \{0, 1\}\): Predetermined value in cell $(r,c) \in \mathcal{I}$.
Decision variables
\(x_{rc} = 1\) if the cell at row $r \in \mathcal{R}$ and column $c \in \mathcal{C}$ contains a $1$, and $0$ if it contains a $0$.
\(z_{rr'c} \in \{0,1\}\): Auxiliary variable indicating whether row $r \in \mathcal{R}$ and row $r’ \in \mathcal{R}$ differ in column $c \in \mathcal{C}$ ($1 \leqslant r < r’ \leqslant n$).
\(w_{cc'r} \in \{0,1\}\): Auxiliary variable indicating whether column $c \in \mathcal{C}$ and column $c’ \in \mathcal{C}$ differ in row $r \in \mathcal{R}$ ($1 \leqslant c < c’ \leqslant n$).
Since we aim to find a valid configuration (feasibility problem), the objective function is arbitrary:
Objective function
\[\min z = 0\]
We now define the Binary Sudoku rules as constraints:
At most two consecutive identical digits (no horizontal or vertical trios):
\[1 \leqslant x_{rc} + x_{r,c+1} + x_{r,c+2} \leqslant 2 \quad \forall r \in \mathcal{R}, \; \forall c \in \{1,\dots,n-2\}\] \[1 \leqslant x_{rc} + x_{r+1,c} + x_{r+2,c} \leqslant 2 \quad \forall r \in \{1,\dots,n-2\}, \; \forall c \in \mathcal{C}\]
No two identical rows:
\[z_{rr'c} \geqslant x_{rc} - x_{r'c} \quad \forall 1 \leqslant r < r' \leqslant n, \; \forall c \in \mathcal{C}\] \[z_{rr'c} \geqslant x_{r'c} - x_{rc} \quad \forall 1 \leqslant r < r' \leqslant n, \; \forall c \in \mathcal{C}\] \[\displaystyle \sum_{c \in \mathcal{C}} z_{rr'c} \geqslant 1 \quad \forall 1 \leqslant r < r' \leqslant n\]
No two identical columns:
\[w_{cc'r} \geqslant x_{rc} - x_{rc'} \quad \forall 1 \leqslant c < c' \leqslant n, \; \forall r \in \mathcal{R}\] \[w_{cc'r} \geqslant x_{rc'} - x_{rc} \quad \forall 1 \leqslant c < c' \leqslant n, \; \forall r \in \mathcal{R}\] \[\displaystyle \sum_{r \in \mathcal{R}} w_{cc'r} \geqslant 1 \quad \forall 1 \leqslant c < c' \leqslant n\]
Variable domains
\[x_{rc} \in \{0,1\} \quad \forall r \in \mathcal{R}, \; \forall c \in \mathcal{C}\] \[z_{rr'c} \in \{0,1\} \quad \forall 1 \leqslant r < r' \leqslant n, \; \forall c \in \mathcal{C}\] \[w_{cc'r} \in \{0,1\} \quad \forall 1 \leqslant c < c' \leqslant n, \; \forall r \in \mathcal{R}\]
Source code
The Python implementation of the optimization model presented in this post, as well as the instance used in the example, are available in the GitHub repository.
Want to keep exploring the world of Operations Research? Discover more posts on the topic here.
If you found this useful, please cite this as:
Martín-Campo, F. Javier (Jul 2026). Beyond 0 and 1, the binary sudoku. https://www.fjmartincampo.com/blog/2026/binarysudoku/.
or as a BibTeX entry:
@misc{martín-campo2026beyond-0-and-1-the-binary-sudoku,title={Beyond 0 and 1, the binary sudoku},author={Martín-Campo, F. Javier},year={2026},month={Jul},url={https://www.fjmartincampo.com/blog/2026/binarysudoku/}}
Enjoy Reading This Article?
Here are some more articles you might like to read next: