Solving Nonograms
CS221 Fall 2014-2015 Project Final
SUNet ID: ilona33
Name: Ilona Kirsanova
Problem Definition
It is fun to solve crosswords, Sudoku or other puzzles. The puzzles I enjoyed since my childhood are
Japanese crosswords or also known as nonograms.
Nonogram is a picture logic puzzles in which cells in a
grid must be colored or left blank according to numbers at the side of the grid to reveal a hidden picture.
The input of a system is 2 lists of lists of integer
that present horizontal and vertical numbers of colored cells. The output is a matrix of boolean, where
T rue is a colored square and F alse is a blank one.
The result should give us a reasonable picture in
a reasonable time.
Solving nonogram puzzles is an NP-complete
problem. This means that there is no polynomial time
algorithm that solves all nonogram puzzles. The challenge is to solve big nonogram fast enough.
The metric of success would be time of resolving
puzzle, the puzzle should match horizontal and verFigure 1: Nonogram
tical numbers of colored cells, and the result should
look like a picture of something.
The oracle is some fantastic algorithm that solves every nonogram in polynomial time.
Search Problem
For the baseline created search problem model:
State: 2-dimensional array of boolean and position in this array.
StartState: empty array and position (0,0).
Actions(s): color or discolor current cell and move to the next cell.
Cost(s,a): infinity, if any row or column is finished and its sum is not the same as input’s
one; 1 otherwise.
IsGoal(s): the position is after the last cell of nonogram.
1
The model solved using UCS search:
Add StartState to frontier (priority queue)
Repeat until frontier is empty:
Remove s with smallest priority p from frontier
If IsGoal(s): return solution
Add s to explored
For each action a in Actions(s):
Get successor s’=Succ(s)
If s’ already in explored: continue
Update frontier with s’ and priority p+Cost(s,a)
This baseline solves this simple puzzle with size of 42 cells in 1 second. But it cannot
solve puzzles size more than 70 cells.
For optimization used A*. Created heuristic as a sum of differences between current
vertical and horizontal numbers of colored cells with the desired ones. This heuristic is
consistent:
1. h(sgoal ) = 0, as in goal state all differences are zero.
2. Cost(s, a) ≥ h(Succ(s, a)) − h(s), as heuristic is minimal number of action we need to
do to get goal
A* optimized UCS, so that we can solve nonograms size up to 81cells. Test results are
shown in the Figure 2.
Figure 2: Search problem statistics
2
Constraint Satisfaction Problem
A nonogram problem designed as constraint satisfaction problem.
Constraint satisfaction problems are just a special case of factor graphs where each of the
potentials returns either 1 or 0. Such a potential is a constraint, where 1 means the constraint
is satisfied and means 0 thatQit is not. Our goal is to find any consistent assignment (if one
exists), where W eight(x) = m
j=1 fj (x) = 1
As input we have a nonogram of size n ∗ m. Let Hi be the horizontal numbers of
colored cells, where i ∈ {1, . . . , n}, and Vj is the vertical numbers of colored cells, where
j ∈ {1, . . . , m}. For every cell let create a CSP variable Xij , where i ∈ {1, . . . , n} is row
index, and j ∈ {1, . . . , m} is column index. Xij ∈ {0, 1}, 1 is colored cell, 0 otherwise.
For horizontal restrictions we introduce auxiliary variable Ahij for i ∈ {1, . . . , n} and
j ∈ {1, . . . , m}. Each Ahij takes on a value which is an input-output pair, where the output
is the result of taking input and incorporating Xij . Ahij (0) is an input number of colored
cells, Ahij (1) is a value of previous cell, Ahij (2) is a value of current cell, Ahij (3) is an output
number of colored cells, for example Ahij = ({3}, 0, 1, {3, 1}).
Let’s make domain restrictions for Ahij for better performance. In domain we set
Ahij (0) = Ahij (3) if Ahij (2) = 0, otherwise, if Ahij (1) = 1 we increment the last value
of Ahij (0) and set the result to Ahij (3), otherwise
we add 1 to Ahij (0) and set it to Aij (3).
P
So created domain size is Domain(Ahij ) = h∈Hi (3 + 3h) + 1.
Added the following constraints:
• Unary potential for initialization on Ahi1 :
[Ahi1 (0) = {} and Ahi1 (1) = 0].
• Binary potentials for adding between Ahij
and Xij : [Ahij (2) = Xij ].
• Binary potentials for consistency between
Ahij and Ahi(j+1) :[Ahij (2) = Ahi(j+1) (1)
and Ahij (3) = Ahi(j+1) (0)].
• Unary potential for the original constraint
on Ahin : [Ahin (3) = Hi ].
The same way for vertical restrictions we create
an auxiliary variable Avij for i ∈ {1, .P
. . , n} and
j ∈ {1, . . . , m}. Domain(Avij ) =
v∈Vi (3 +
3v) + 1.
Figure 3: CSP model
Added the following constraints:
• Unary potential for initialization on Av1i : [Av1j (0) = {} and Av1j (1) = 0].
• Binary potentials for adding between Avij and Xij : [Avij (2) = Xij ].
3
• Binary potentials for consistency between Avij and Av(i+1)j :[Avij (2) = Av(i+1)j (1) and
Avij (3) = Av(i+1)j (0)].
• Unary potential for the original constraint on Avmj : [Avmj (3) = Vj ].
Whole CSP model is shown on Figure 3. Treewidth of this CSP model is min(n, m).
Solving CSP Approaches
First of all to solve CSP used backtracking search:
Backtrack(x,w,Domains):
If x is complete: update best and return
Choose unassigned variable X
Order values Domain of choosen X
For each value v in that order:
Count weight w’
If w’ = 0: continue
Domains’ is Domains updates with AC-3
x’ is x with X assigned to v
Backtrack(x’, ww’, Domains’)
Backtracking search shows good results, it is quick in practice, does not need much space.
This approach is much better than baseline, and can solve nonograms of size up to 1575 cells.
To resolve bigger nonograms created 2 simple linear heuristics for backtracking search.
1. Method uses conjunctions of possible places for each block of boxes. For example, in
a row of ten cells with only one clue of 8, the bound block consisting of 8 boxes could
spread from block of boxes.
2. Method fills cells on the bounds of the nonogram. For example, in a row of ten cells
with clues of 3 and 2, the block consisting of 3 boxes has already began in the left as
well as block consisting of 2 in the right.
4
Backtracking search with heuristics works much better. It can solve nonograms of size
up to 3000 cells. All tests results are shown on Figure 4. It make much progress comparing
with baseline.
Figure 4: Solving nonograms statistics
Variable elimination needs exponential space. Limited amount of memory does not let
to solve nonogram of size more than 18 cells. So cannot compare variable elimination with
other algorithms, but the small nonogram was solved much slower than backtracking search.
Future work
The major improvement can give more heuristics for backtracking search. There are few
simple heuristics:
1. Method determine spaces by searching for cells that are out of range of any possible
blocks of boxes. For example, considering a row of ten cells with boxes in the fourth
and ninth cell and with clues of 3 and 1, the block bound to the clue 3 will spread
through the fourth cell and clue 1 will be at the ninth cell.
5
2. In this method, the significance of the spaces will be shown. A space placed somewhere
in the middle of an uncompleted row may force a large block to one side or the other.
Also, a gap that is too small for any possible block may be filled with spaces.
3. Sometimes, there is a box near the border that is not farther from the border than the
length of the first clue. In this case, the first clue will spread through that box and
will be forced outward from the border.
4. Boxes closer to each other may be sometimes joined together into one block or split by
a space into several blocks. When there are two blocks with an empty cell between,
this cell will be a space if joining the two blocks by a box would produce a too large
block; a box if splitting the two blocks by a space would produce a too small block
that does not have enough free cells remaining.
These heuristics can much increase speed of solving nonograms, and solve nonograms of
size more than 3000 cells.
In a big nonograms removing few potentials more likely gives us approximately right
picture. It is worth to do that for increasing speed of solving nonograms. But for backtracking
search to increase speed we need to add constrains not remove. So we need to use another
methods like local search, which can give us approximately right picture quick enough.
Some CSP model modification can let us solve colored nonograms, or even sudoku. But
heuristics used in this project works only for black nonograms, they can be modified for
colored nonograms, and there are other heuristics for sudoku.
6
© Copyright 2026 Paperzz