Chapter 5
Takuzu
Takuzu is the puzzle of balanced coin flips. The grid is a square of even side , each cell shaded or unshaded (heads or tails, black or white, or ). A small number of cells carry given values; the solver fills the rest subject to two constraints. Every row and every column contains equal counts of the two states, and no row or column contains three consecutive cells of the same state.
The puzzle is known under several names: Binairo in European sources, Tohu-Wa-Vohu in the janko.at archive (after the Hebrew for "formless and void"), Takuzu in Japan, Binary Puzzle in English-language newspapers. Nikoli publishes it under one of several Japanese names in the sum/shading family. The earliest version with the three-in-a-row prohibition seems to be due to Adolfo Zanellati circa ; earlier binary Latin-square puzzles without the consecutive constraint existed in recreational literature for decades.
Mathematically, Takuzu is a pure / integer program, like Kakurasu of the previous chapter, but with a richer constraint shape. The equal-counts constraint is one linear equality per row and per column. The no-three-consecutive constraint bans just two local patterns, and ; it translates cleanly into pairs of inequalities on sliding windows of three variables.
Rules and a small instance
A Takuzu puzzle is an grid with even. Each cell holds a value . Two constraints govern the grid.
Every row and every column contains exactly zeros and ones.
No three consecutive cells in any row or column share the same value.
A well-posed Takuzu puzzle has a subset of cells pre-filled with or ; these are the givens. The solver’s task is to extend the partial assignment to the unique complete grid that respects both rules.
Figure 5.1 shows a Takuzu with eight given cells, four white and four black. The unique completion appears in Figure 5.2; givens in the solution are outlined in copper so as to distinguish them from cells the solver filled.
The programming model
Let for . The equal-counts constraint is
The no-three-consecutive constraint is a pair of inequalities on every sliding window of three consecutive cells. For a window to avoid the sum must be at least ; to avoid the sum must be at most :
On an grid this gives sliding-window inequalities plus equal-counts equalities, all linear, all on binary variables. Unlike Kakurasu the constraints are local: each involves only two or three variables. The number of feasible assignments of a raw Takuzu grid (no givens) grows rapidly but slower than ; a typical published puzzle constrains it down to a single solution with only a modest number of givens.
A solver in a dozen lines
from ortools.sat.python import cp_model
def solve_takuzu(n, givens):
"""givens: (r, c, v) triples; v in {0, 1}."""
m = cp_model.CpModel()
x = [[m.NewBoolVar(f"x{r}{c}")
for c in range(n)] for r in range(n)]
for r, c, v in givens:
m.Add(x[r][c] == v)
for r in range(n):
m.Add(sum(x[r]) == n // 2)
for c in range(n):
m.Add(sum(x[r][c] for r in range(n)) == n // 2)
for r in range(n):
for c in range(n - 2):
s = x[r][c] + x[r][c+1] + x[r][c+2]
m.Add(s >= 1); m.Add(s <= 2)
for c in range(n):
for r in range(n - 2):
s = x[r][c] + x[r+1][c] + x[r+2][c]
m.Add(s >= 1); m.Add(s <= 2)
s = cp_model.CpSolver()
ok = s.Solve(m)
if ok not in (cp_model.OPTIMAL, cp_model.FEASIBLE):
raise RuntimeError("no solution")
return [[s.Value(x[r][c])
for c in range(n)] for r in range(n)]
The toy instance solves in about milliseconds.
A hard instance
Figure 5.3 is puzzle from the janko.at Tohu-Wa-Vohu archive, Otto Janko’s own, rated at the top tier of the site’s difficulty scale. The grid is ; fifty-four cells are given, leaving one hundred and forty-two to deduce. The same solver code returns the unique completion in about milliseconds, faster than the toy despite the larger grid: with a denser set of givens the local inequalities propagate earlier and the solver’s search tree is narrower.
The variant with distinct rows and columns
A stricter version of the puzzle, often marketed as Binairo, adds a third rule: no two rows are identical and no two columns are identical. This turns the puzzle into a kind of binary Latin-square relative: the rows and columns are -bit strings that are -balanced and avoid triple repeats, and the grid must be a set of such strings.
The distinctness constraint is cheap to add. For each pair of rows with , require at least one column at which they differ: reified cell-by-cell into booleans. The same for each pair of columns. In the CP-SAT model this adds row-pair constraints and column-pair constraints, each with auxiliary booleans: booleans in total. For that is manageable; for larger than about the model starts to hurt and a dedicated all-different constraint over row-integers is preferable.
Curiously, the janko.at puzzle does not satisfy the distinctness rule: one column appears twice in the solution. Whether the author intended the strict Binairo rules or the three-in-a-row rules alone is a curatorial question. The solver code above uses the latter; switching to the Binairo rules would have rendered the published puzzle infeasible.
Sources. The three-in-a-row prohibition and the equal-counts rule are due to Adolfo Zanellati (Italy, c. ), who marketed the puzzle as Binairo. The Hebrew-language naming Tohu-Wa-Vohu comes from the janko.at archive; puzzle in that archive is by Otto Janko. Nikoli publishes the three-in-a-row variant under various Japanese names. The CP-SAT formulation here follows the natural / ILP approach; for a decision-theoretic study of the puzzle’s complexity, see Utiyama and Uehara, Computational complexity of the Binairo puzzle, Proceedings of IPSJ Workshop on Game Informatics ().