DOI 10.1007/s10489-009-0200-0
An efficient algorithm for solving nonograms
Chiung-Hsueh Yu· Hui-Lung Lee · Ling-Hwei ChenPublished online: 13 November 2009
© Springer Science+Business Media, LLC 2009
Abstract Nonogram is one of logical games popular in Japan and Netherlands. Solving nonogram is a NP-complete problem. There are some related papers proposed. Some use genetic algorithm (GA), but the solution may get stuck in local optima. Some use depth first search (DFS) algorithm, the execution speed is very slow. In this paper, we propose a puzzle solving algorithm to treat these problems. Based on the fact that most of nonograms are compact and contiguous, some logical rules are deduced to paint some cells. Then, we use the chronological backtracking algorithm to solve those undetermined cells and logical rules to improve the search efficiently. Experimental results show that our algorithm can solve nonograms successfully, and the processing speed is significantly faster than that of DFS. Moreover, our method can determine that a nonogram has no solution.
Keywords Japanese puzzle· Nonogram · Depth first search· Chronological backtracking
1 Introduction
Nonogram, also known as Japanese puzzle, is one of log-ical games popular in Japan and Netherlands. The ques-tion “Is this puzzle solvable?” is a NP-complete problem
C.-H. Yu· H.-L. Lee · L.-H. Chen (
)Department of Computer Science, National Chiao Tung University, Hsinchu, Taiwan, ROC
e-mail:lhchen@cc.nctu.edu.tw C.-H. Yu
e-mail:Sharon@debut.cis.nctu.edu.tw H.-L. Lee
e-mail:huilung@debut.cis.nctu.edu.tw
[1,2]. Some related papers [3, 4] solved this problem by non-logical algorithms, and the execution speed is slow. In the following, we will give a brief review.
1.1 Nonograms
Figure1(a) shows a simple nonogram. The positive integers in the top of a column or left of a row stand for the lengths of black runs in the column or row respectively. The goal is to paint cells to form a picture that satisfies the following constraints:
1. Each cell must be colored (black) or left empty (white). 2. If a row or column has k numbers: s1, s2, . . . , sk, then
it must contain k black runs—the first (leftmost for rows/topmost for columns) black run with length s1, the
second black run with length s2, and so on.
3. There should be at least one empty cell between two con-secutive black runs.
It is evident that the puzzle in Fig.1(a) has a unique so-lution shown in Fig.1(b). However, the puzzle in Fig.2has two solutions, Fig.2(b) is the first solution, Fig.2(c) is the second solution, and the puzzle in Fig.3has no solution (i.e. no corresponding picture). Hence, there may be none, exact one, or more than one solution for given integral numbers. Note that a puzzle solution can be considered as a black-white picture. Here, we use as a colored (black) cell, as an empty (white) cell, and as an unknown cell (i.e. an undetermined cell).
1.2 Constraint satisfaction problem
Formally speaking, the constraint satisfaction problem (CSP) is composed of a finite set of variables, X1, X2,
. . . , Xn, each of Xi is associated with a domain Di of
pos-sible values, and a set of constraints, C1, C2, . . . , Cn [5].
The solution of CSP is to find a legal assignment that any variable will be assigned a value without violating any con-straint. In fact, a nonogram can be modeled as a CSP. Each cell in a nonogram is a variable with domain: black, white or grey. The chronological backtracking (CB) is a depth first search based algorithm and commonly used for solv-ing CSP. The algorithm chooses one variable at a time and backtracks to the last decision when it becomes unable to proceed [5]. For obtaining the solution efficiently, CB
algo-Fig. 1 Nonogram. (a) A simple puzzle. (b) The solution of (a)
Fig. 2 A puzzle with two solutions. (a) A puzzle example. (b) The first solution. (c) The second solution
rithm with look ahead strategy will be applied to reduce the search space and detect illegal assignment.
1.3 Previous works
In 2003, Batenburg [3] proposed an evolutionary algorithm for discrete tomography (DT). DT is concerned with the re-construction of a discrete image from its projections [4]. Nonograms can be considered as a special form of the DT problem. Figure4 shows an example to explain the differ-ence between them. Batenburg and Kosters [4] modified the evolutionary algorithm to solve nonogram. Since the evolu-tionary algorithm in [3] will converge to a local optimum, the obtained solution may be incorrect.
In 2004, Wiggers [6] proposed a genetic algorithm (GA) and a depth first search (DFS) algorithm to solve nonograms. He also compared the performance of these two algorithms. For a puzzle of small size, DFS algorithm is faster than GA; otherwise, GA is faster. However, both methods are slow, and the GA algorithm may get stuck in local optima. In the following, we will give a brief description for the DFS algo-rithm.
1.4 Depth first search (DFS)
In DFS, all possible solutions of each row are generated. Figure5gives an example to illustrate the DFS algorithm. Figure5(a) shows a nonogram. Figure5(b) shows the cor-responding DFS tree of Fig.5(a), (r.i1, . . . , r.ij, . . . , r.ik)in
each node stands for the first black run of row r starting at position i1, and the j th black run of row r starting at
posi-tion ij. Figure5(c) shows all possible solutions in the tree.
Each possible solution corresponds to a path from root to a leaf node, find all paths using DFS and then use the col-umn information (the numbers in the top of each colcol-umn) of the puzzle to verify each possible solution. A possible so-lution will be considered as a true soso-lution, if it satisfies all columns’ restrictions. Through the verification process, we
Fig. 3 A puzzle with no solution. (a) A puzzle example. (b) If the first five cells are black in row 3, there is no solution in rows 2 and 4. (c) If the last five cells are black in row 3, there is no solution in row 4
Fig. 4 DT problem and nonogram. (a) DT problem. (b) Nonogram
can see that only the fourth possible solution in Fig.5(c) is the true one.
Note that both of the evolutionary algorithm presented in [4] and the genetic algorithm (GA) described in [6] will converge to a local optimum, so sometimes the obtained so-lution may get stuck in local optima. Although the DFS al-gorithm proposed in [6] can always find a correct solution, it takes much longer time than GA when solving large puz-zles. To solve these problems, in this paper, we consider the nonogram as a CSP problem. The CB algorithm will be ap-plied to search for solution. In order to speed up the search, some logical rules (LR) are considered as CSP filters to de-termine as many unknown cells as possible in a nonogram. On the other hand, LRs are used for detecting unsatisfiable cases and reducing the search space.
The remainder of this paper is organized as follows. The proposed algorithm is presented in Sect.2. In Sect.3, sev-eral experimental results are shown. Finally, conclusions are made.
2 Proposed method
In a general nonogram game, we usually paint those cells which can be determined immediately at first. Then, the rest of undetermined cells will be solved by guess. Based on this fact, we propose a method to solve nonograms automati-cally. At first, a puzzle will be solved by logical rules until all logical rules can not be applied. Then, the proposed method will use CB to visit a possible solution (an internal node) in a row and the column information is used to do verification. All logic rules will be immediately applied first before we use CB to visit the next node. The proposed method will re-currently run these steps until we obtain a solution (a leaf node).
2.1 The first phase: logical rules (LR)
One may use some rules [7] to solve nonograms. In this phase, eleven rules with a new concept of range of a black run are proposed. These rules can be divided into three main parts. The first part is used to determine which cells should be colored or left empty, the second part is used to refine the ranges of black runs, and the third is used to determine which cells should be colored or left empty and to refine the ranges of black runs.
Note that in [7], the rules of “ ” and “ ” are similar to our rules. Some are composed of our several rules. Some are the same as ours. Our pro-posed rules 1.3, 1.4, 2.1, 2.3, 3.1, 3.3-2, and 3.3-3 are new idea. And our rules do not include the rules of “ ” in [7] since the rules of “ ” provide an undeter-mined guess. In our method, our rules are used for reducing the search space. Hence, the rules must provide a determined answer to eliminate some cases.
In the beginning, all cells in a puzzle are considered as unknown. Then each rule is applied in each row and then in each column. The total eleven rules are executed sequen-tially and iteratively. In some iterations, some unknown cells will be determined. However, in some iterations, maybe only the ranges of some black runs are refined. Thus, if no unknown cell is determined and no black run’s range is changed, we will stop using logical rules and start to apply CB algorithm.
Since all rules are applied to each row and column, we only take a row as an example to explain the proposed algo-rithm.
Preliminary for run range
The position where a black run may be placed plays an im-portant role. An idea about the range (rj s,rj e)of a black
run j is proposed, where rj sstands for the left-most
possi-ble starting position of run j , and rj e stands for the
right-most possible ending position. That is, black run j can only be placed between rj s and rj e. If the range of each black
run is estimated more precisely, we can solve puzzle more quickly. Note that for each black run, we must reserve some cells for the former black runs and the later ones. Figure6 gives an example to illustrate the idea. Figure6(a) shows a special row with three black runs of lengths 1, 3, and 2, re-spectively. Figure6(b) shows the left-most possible solution
Fig. 5 An example to illustrate the DFS algorithm for solving nonogram. Only solution (4) is correct
and the left-most possible starting position of each run. Fig-ure6(c) shows the right-most possible solution and the right most possible ending position of each run.
Thus, in the beginning, the initial range of a black run in a row is set between its left-most possible position and its right-most possible position. In the following, we will give
Fig. 6 An illustration of an initial black run range. (a) A special row of a puzzle. (b) The left-most possible solution of (a). (c) The right-most possible solution of (a)
a formal formula to calculate the initial run range of each black run.
Initial run range estimation
Let the size of each row with k black runs be n and the cells in the row with index (0, . . . , n− 1), we can use the follow-ing formula to determine the initial range of each black run.
r1s= 0, rj s= j−1 i=1 (LBi+ 1), ∀j = 2, . . . , k rj e= (n − 1) − k i=j+1 (LBi+ 1), ∀j = 1, . . . , k − 1 rke= n − 1 (1)
where LBi is the length of black run i.
Using formula (1), we can get the initial ranges of the three black runs in Fig. 6(a), which are (0, 2), (2, 6), and (6, 9), respectively.
Rules in Part I
There are five rules in this part, all are used to determine which cells to be colored or left empty.
Rule1.1
For each black run, those cells in the intersection of all the possible solutions of the black run must be colored. In fact, the intersection of all possible solutions is the intersection of the left-most possible solution of the black run and the right-most possible solution of the black run. It is obvious that the intersection exists when the length of the black run’s range is less than two times the actual length of the black run. Figure7shows an example. Consequently, we provide Rule1.1to paint the cells sure to be colored. In the rule, ci
stand for the cell with index i.
Rule 1.1 For each black run j , cell ci will be colored when
rj s+ u ≤ i ≤ rj e− u, where u = (rj e− rj s+ 1) − LBj
Fig. 7 An example of Rule1.1
Rule1.2
When a cell does not belong to the run range of any black run, the cell should be left empty. Rule1.2is provided to do this work.
Rule 1.2 For each cell ci, it will be left empty, if one of the
following three conditions is satisfied (1) 0≤ i < r1s,
(2) rke< i < n,
(3) rj e< i < r(j+1)s for some j, 1≤ j < k.
Rule1.3
For each black run j , when the first cell crj s of its run range is colored and covered by other black runs, if the lengths of those covering black runs are all one, cell crj s−1should be left empty. Similarly, when the last cell crj e is colored and covered by other black runs, if the lengths of those cover-ing black runs are all one, cell crj e+1should be left empty. We provide Rule1.3to determine whether cells crj s−1and crj e+1should be left empty. Taking Fig.8as an example, the colored cell crj s in Fig.8(a) is the starting cell of the range of the last black run with length 3. It is also covered by the third black run with length 1. Thus, it must be the third black run with length 1 (see Fig.8(b)) or the head cell of the last black run (see Fig.8(c)). No matter what case, the cell crj s−1 should be left empty.
Rule 1.3 For each black run j , j= 1, . . . , k
(1) If the lengths of all black run i covering crj s with i= j are all one, cell crj s−1will be left empty.
Fig. 8 An example of Rule1.3. (a) One row in a puzzle with a partial painting result. (b) The cell crj sbelongs to the third
black run with length one. (c) The cell crj s is the head cell
of the last black run
Fig. 9 An example of Rule1.4. (a) One partial painting row of a
puz-zle with max L= 3. (b) The new black segment length after coloring
ciis 4 > 3. (c) The cell cishould be left empty
(2) If the lengths of all black run i covering crj ewith i= j are all one, cell crj e+1will be left empty.
Rule1.4
There may be some short black segments in a row. If two consecutive black segments with an unknown cell between them (see Fig.9(a)) are combined into a new black segment (see Fig. 9(b)) with length larger than the maximal length max L of all black runs containing part of this new seg-ment, the unknown cell should be left empty (see Fig.9(c)). Rule1.4is provided to deal with this situation.
Rule 1.4 For any three consecutive cells ci−1, ci, and
ci+1, i= 1, . . . , n − 2.
Let max L be the maximal length of all black runs con-taining the three cells.
Assumption: cells ci−1and ci+1are black, cell ci is
un-known. If we color ci and find that the length of the new
black segment containing ci is larger than max L, ci should
be left empty.
Rule1.5
Some empty cells like walls may obstruct the expansion of some black segments, we can use this property to color more cells. Figure10gives an example. In this figure, we do not know ci belonging to which black run. However, an empty
cell ci−2 obstructs the black segment containing ci to
ex-pand to the left side of ci−2. Hence, no matter ci belongs
to the run with length 3 or the run with length 4, cell ci+1
should be colored.
On the other hand, for a black segment covered by a se-ries of black runs, which have the same length and overlap-ping ranges, if the length of the black segment equals to the length of those black covering runs, the two cells next to the two ends of the black segment are set as empty (see Fig.11). Rule1.5is proposed to deal with the above two situations. Rule 1.5 For any two consecutive cells ci−1 and ci, i=
1, . . . , n− 1.
Constraint: cell ci−1must be empty or unknown, and cell
ci must be black.
Fig. 10 An example of Rule1.5. Top: one partial painting row of a puzzle.
Middle: all possible solutions
for ci. Bottom: the result of
Fig. 11 An example of Rule1.5. Top: all black runs containing ciand ci+1have the same length 2. Bottom: ci−1and ci+2are left empty after applying Rule1.5to top figure
1. Let min L be the minimal length of all black runs cover-ing ci.
2. Find an empty cell cm closest to ci, m∈ [i − min L +
1, i− 1]. If cmexists, color each cell cpwith i+ 1 ≤ p ≤
m+ min L.
3. Find an empty cell cn closest to ci, n ∈ [i + 1,
i+ min L − 1]. If cn exists, color each cell cp with
n− min L ≤ p ≤ i − 1.
4. If all black runs covering ci have the same length as that
of the block segment containing ci.
(1) Let s and e be the start and end indices of the black segment containing ci
(2) Leave cells cs−1and ce+1empty.
Rules in Part II
This part contains three rules, which are designed to refine the ranges of black runs.
Rule2.1
For two consecutive black runs j and j+ 1, the start (end) point of run j should be in front of the start (end) point of run j+ 1. Based on this property, Rule2.1is provided to update the range of each black run j with rj s≤ r(j−1)s or
rj e≥ r(j+1)e.
Rule 2.1 For each black run j , set
rj s= (r(j−1)s+ LBj−1+ 1), if rj s≤ r(j−1)s rj e= (r(j+1)e− LBj+1− 1), if rj e≥ r(j+1)e
Rule2.2
There should have at least one empty cell between two con-secutive black runs, so we should update the range of black run j if cell crj s−1or crj e+1is colored. Rule2.2is proposed to treat this situation.
Fig. 12 An example of Rule2.3
Rule 2.2 For each black run j , set
rj s= (rj s+ 1), if cell crj s−1is colored rj e= (rj e− 1), if cell crj e+1is colored
Rule2.3
In the range of a black run j , maybe one or more than one black segment exist. Some black segments may have lengths larger than LBj, but some not. For each black segment with
length larger than LBj, if we can determine that it belongs
to the former black runs of run j or the later ones, we can update the range of black run j . Rule2.3is provided to do this work. Figure12gives an example, the original range of the second black run is (4, 11). In (4, 11), the length of the first black segment, FS, is 3 which is larger than the length (2) of the second black run, FS belongs to the first black run. The range of the second black run can be updated to (8, 11). Rule 2.3 For each black run j , find out all black segments in (rj s, rj e). Denote the set of these black segments by B.
For each black segment i in B with start point isand end
point ie. If (ie− is+ 1) is larger than LBj, set
⎧ ⎪ ⎪ ⎨ ⎪ ⎪ ⎩
rj s= (ie+ 2), if black segment i only belongs to the
former black runs of run j
rj e= (is− 2), if black segment i only belongs to the
later black runs of run j
Rules in Part III
This part is composed of three rules. The purpose of each rule is not only to determine which cells should be colored or left empty but also to refine the ranges of some black runs.
Rule3.1
When several colored cells (see Fig.13(a)) belonging to the same black run are scattered, all unknown cells among them should be colored to form a new black segment, and the run range can also be updated (see Fig.13(b)). Rule3.1is pre-sented to treat this situation.
Rule 3.1 For each black run j , find the first colored cell cm
Fig. 13 An example of Rule3.1. (a) One partial painting row of a puzzle. (b) The result of applying Rule3.1to (a)
Fig. 14 An example of Rule3.2. (a) One partial painting row of a puzzle. (b) The result of applying Rule3.2to (a)
Fig. 15 An example of Rule3.3-1. (a) One partial painting row of a puzzle. (b) The result of applying Rule3.3-1to (a)
all cells between cmand cn, and set
rj s= (m − u)
rj e= (n + u)
where u= LBj− (n − m + 1)
Rule3.2
As shown in Fig. 14(a), some empty cells may be scat-tered over the range of black run j , so there will be sev-eral segments bounded by these empty cells. The lengths of some segments may be less than LBj, these segments can
be skipped and the run range can be updated. Rule 3.2is proposed to solve this situation. Figure14(a) shows six seg-ments in 3’s range. The first two and the last segseg-ments with lengths less than 3 will be skipped and the run range can be adjusted. Since the length of the forth segment is less than 3 and is covered only by one black run with length 3, it should be empty. Figure14(b) shows the result of applying Rule3.2 to Fig.14(a).
Rule 3.2 For each black run j , find out all segments bounded by empty cells in (rj s, rj e). Denote the number
of these segments to be b and index them as 0, 1, . . . , b− 1. Step 1. Set i= 0.
Step 2. If the length of segment i is less than LBj, i= i + 1
and go to step 2. Otherwise, set rj s= the start index
of segment i, stop and go to step 3. Step 3. Set i= b − 1.
Step 4. If the length of segment i is less than LBj, i= i − 1
and go to step 4. Otherwise, set rj e= the end index
of segment i, stop and go to step 5.
Step 5. If there still remain some segments with lengths less than LBj, for each of this kind of segments, if the
segment does not belong to other black runs, all cells in this segment should be left empty
Rule 3.3
This rule is designed for solving the situations that the range of black run j do not overlap the range of black run j− 1 or j+ 1. First, if black run j does not overlap the range of black run j− 1, consider the following three cases: Case 1: Cell crj s is black (see Fig.15(a)).
We can finish this black run (see Fig.15(b)). Rule3.3-1 is provided to treat this situation.
Rule 3.3-1 For each black run j with Crj s colored and its range not overlapping the range of black run j− 1,
Fig. 16 An example of Rule3.3-2. (a) One partial painting row of a puzzle. (b) The result of applying Rule3.3-2to (a)
Fig. 17 An example of Rule3.3-3. (a) One partial painting row of a puzzle. (b) The result of merging two black segments. (c) The result of applying Rule3.3-3to (a)
(1) Color cell Ci, where rj s+ 1 ≤ i ≤ rj s+ LBj− 1 and
leave cell Crj s−1and Crj s+LBiempty (2) Set rj e= (rj s+ LBj− 1)
(3) If the range of black run j+ 1 overlaps the range of black run j , set r(j+1)s= (rj e+ 2)
(4) If r(j−1)e= rj s−1, r(j−1)e= rj s−2
Case 2: An empty cell cw appears after a black cell cb(see
Fig.16(a)).
It should be true that each cell after cw will not belong
to black run j . Rule3.3-2is provided to refine the range of black run j as shown in Fig.16(b).
Rule 3.3-2 For each black run j with its range not over-lapping the range of black run j− 1, if an empty cell cw
appears after a black cell cbwith cw and cbin the range of
black run j . Set rj e= w − 1
Case 3: There is more than one black segment in the range of black run j (see Fig.17(a)).
In (rj s, rj e), find the first and second black segments. If
the length of the new run after merging these two black seg-ments by coloring those cells between these two segseg-ments is larger than LBj, then these two segments should not belong
to the same run. Otherwise, keep checking the length of the new run after merging the first and third black segments. The process will be repeated until all black segments in (rj s,rj e)
have been checked or a black segment i is found such that the length of the new run after merging the first black seg-ment and black segseg-ment i is larger than LBj. Rule3.3-3is
provided to deal with this situation. Figure17gives an ex-ample. In Fig.17(a), the black run with length 4 covers two black segments. The length after merging these two black
segments is 5 (see Fig.17(b)), which is larger than 4, thus we can update the run range as shown in Fig.17(c). Rule 3.3-3 For each black run j with its range not overlap-ping the range of black run j− 1, if there is more than one black segment in the range of the black run j , find out all black segments in (rj s,rj e).
Denote the number of these black segments to be b and index them as 0, 1, . . . , b− 1.
Step 1. Set i= 0
Step 2. Find the first black cell cs in black segment i
Step 3. Set m= i + 1
Step 4. If m < b, find the first black cell ctand the end black
cell cein black segment m,
If (e− s + 1) > LBj, stop and set rj e= t − 2. Otherwise,
m= m + 1 and go to step 4.
If the range of black run j does not overlap the range of black run j+ 1, we can use the similar way to treat this situation.
2.2 The second phase: chronological backtracking with LR filter
The chronological backtracking (CB) is a depth first search based algorithm [5] and commonly used for solving CSP. CB is an exhaustive search, so it can find the puzzle solution eventually. Since the CB is time-consuming, here we use the LR to raise the processing speed.
One thing should be mentioned at first, we use row in-formation to build a DFS tree, and the column inin-formation is used immediately to do verification when a row is used to create a layer. It means that every layer of the DFS tree is composed of row information, and all nodes in each layer
Fig. 18 An example of chronological backtracking with LR filter. (a) A given puzzle. (b) The result after applying LRs to (a). (c) Seven possible solutions for row 1. (d) The result after visiting node PS11
and doing column verification. (e) The result after applying LRs to (d). (f) Ten possible solutions for row 2. (g) The result after visiting node PS21
and doing column verification. (h) The result after applying LRs to (g). (i) The result after visiting the next node (the first possible solution in row 3) of the DFS tree and doing column verification. (j) The result after applying LRs to (i)
are the possible solutions (PS) for the corresponding row. Figure18gives an example for CB with LR filter.
First, we run the LRs to the nonogram of Fig.18(a) until LRs can not be applied. We obtain the result as shown in Fig.18(b). There are seven possible solutions for row 1 (see Fig.18(c)). Next, we visit the first possible solution PS11of
row 1 in layer 1 of the DFS tree. Then the column infor-mation is used to do verification. We will obtain the result as shown in Fig.18(d). Subsequently, we apply the LRs to Fig.18(d) and the result is shown as Fig.18(e). There are ten possible solutions for row 2 (see Fig.18(f)). Then, we visit the first possible solution PS21 of row 2 in layer 2 of the
DFS tree.We use the column information to do verification.
We obtain the result as shown in Fig.18(g). Subsequently, we apply the LRs to Fig.18(g) and the result is shown as Fig.18(h). We recurrently apply the above procedure to the
remaining rows until the last row is processed. The final re-sult is shown in Fig.18(j).
3 Experimental results
To do experiment, we collect 264 puzzles. Most of them come from [4,6–8] and few are created by us. A PC (CPU: AMD Athlon 2600+ 1.92 GHz) is used to run the proposed method and the methods GA and DFS provided in [6].
Because most nonograms are meaningful, they can be solved quickly and completely only by logical rules. The execution time is less than 1 second. If a puzzle cannot be solved deterministically using logical rules, the unknown part left can be solved successfully by CB with LR filter. Figure19shows some test image. Using the methods pro-vided in [6], Figs. 19(a), (b) and (d) take more than 20
Fig. 18 (continued)
minutes to get the solutions, even an incorrect solution, but using our proposed logical rules, only about 0.1 second is needed to solve them correctly. Using the proposed method, Figs.19(c) and (f), which are random images (50% black), needs 13.1 and 23.4 seconds respectively to get the solu-tions, the speed is slow. The reason is that when black cells in a puzzle scatter everywhere, the LRs will fail to solve this kind of puzzles in the first time to apply LRs. Furthermore, in CB with LR filter, since there are many short black runs in each row, this produces many possible solutions in each row and we should check all of them. However, using the GA or DFS provided in [6], more than 2 days is needed, our method is still faster.
On the other hand, Fig. 19(e) is solved successfully within 4.82 minutes using the proposed method. The
rea-son is that there are many short black runs in a row, that is, many possible solutions should be checked in the DFS tree. Figure20from [4] is a puzzle with two solutions. After applying our algorithm, all solutions are found. Figure21 shows a puzzle with no solution, using GA [6] will get a local optimal answer (see Fig.21(b)). However, our method can detect it quickly. We use a scheme that when one cell is determined as a colored cell, but it has been left empty, the puzzle is considered to have no solution. Similarly, if one cell is determined as an empty cell, but it has been colored, the puzzle will also be considered to have no solution.
Finally, Fig.22 shows some other test images and Ta-ble1shows the comparison of the experimental results us-ing the methods proposed in [6] and our proposed algorithm.
Fig. 19 Test images. (a) Sheep (25× 25). (b) Airplane (25× 25). (c) Random_1 (30× 30). (d) Monkey (15× 15). (e) Sunflower (25× 25). (f) Random_2 (30× 30)
Fig. 20 A 7× 8 puzzle with
two solutions. (a) The result after applying logical rules. (b) The first solution. (c) The second solution
Fig. 21 A puzzle with no solution. (a) The original puzzle. (b) A local optimal answer obtained using GA
Fail denotes the case where the executing time is more than 24 hour. From Table 1, we can see that over 93% puzzles take more than 1 minute using GA or DFS. However, apply-ing our proposed method, over 98% puzzles need less than 0.6 seconds. All puzzles in our database are solved
success-fully and correctly. The detail results are shown in Table1. Note that our decision heuristic used in the search is lex-icographical (i.e. selects sequentially row by row starting from the first). We have also done other experiments using the most conflicting heuristic of starting with the row with
Fig. 22 Test images. (a) Flower_word (10× 10). (b) Hippo (20× 20). (c) Formosa (25× 25). (d) Snoopy (25× 25). (e) Owl (30× 25). (f) Skating (30× 25)
Table 1 The comparison of the experimental results among methods in [6] and our algorithm
Puzzle size: Number of puzzles (264 puzzles) Execution time
GA DFS Our method
5× 5:1, 5 × 6:1 Local optimal answer >1 min No solution <0.07 sec
No solution <0.032 sec
10× 10:2, 30 × 40:1 Local optimal answer >1 min No solution >1 min
(above five puzzles have no solution)
6 × 6:7 <6 sec <0.7 sec <0.048 sec
6× 6–10 × 10:9 30 sec:1, >1 min:8 <1 min:7, >1 min:2 <0.048 sec
10× 10–15 × 15:33 15× 15–25 × 25:111
25 × 25:99
Random_1 Fail Fail 13.1 sec
Sunflower >1 hr >1 hr 4.82 min
Random_2 >1 hr >1 hr 23.4 sec
Others >1 min >1 min
Owl: 0.25 sec Skating: 0.359 sec Others: <0.517 sec
tighter constraints (i.e. higher numbers). The result shows that the search with the most conflicting heuristic does not perform better than with the lexicographical heuristic. In our method, rows are processed sequentially, once the col-umn information is used to do verification, some cells in the next row according to the column information may be deter-mined; this will reduce the size of the DFS tree. For exam-ple, in Fig.23, if cell i in the first row and cell j in the sec-ond row are colored, we can certainly determine that cell k in the third row should be colored. If we select the row with the higher number, then row 6 and row 4 will be processed first. Since these two do not adjoin, the column information can not be used to determine the cells in rows 3, 5, or 7. In Fig.23, if cell o and m are colored, we can not determine cell n.
4 Conclusions and discussion
In this paper, we have proposed a fast method to solve nono-grams. The method first applies the LRs to solve nonograms iteratively until all LRs can not be applied. Most nonograms can be solved by only applying LRs. If it can not be solved completely, the proposed method will use CB with LR filter to solve those unknown cells.
The experimental results show that our method can solve those puzzles with compact black patterns quickly. For those puzzles with random black patterns, the method can also raise the speed of DFS using the pruning scheme. Further-more, our method can always provide correct solutions and is better than the methods provided in [6].
Since the DT problem can be considered as a general form of nonograms, extending our method to solve the DT
Fig. 23 An example to illustrate the search with the most conflict heuristic and the lexicographical heuristic
problem is our future work. At first, a DT problem can be decomposed into many kinds of corresponding nonograms. Then these nonograms are considered as the input of our method, and our method will provide all of the correct solu-tions. Moreover, our method can determine that a nonogram has no solution. The number of the corresponding nono-grams is very large, and most nononono-grams have no solution and take a lot of processing time, the key challenge of our future work is to reduce the number of nonograms corre-sponding to a given DT.
References
1. Ueda N, Nagao T (1996) NP-completeness results for NONO-GRAM via parsimonious reductions. Technical report TR96-0008, Department of Computer Science, Tokyo Institute of Technology, May 1996
2. McPhail BP (2005) Light up is NP-complete. Feb 2005. http:// www.reed.edu/~mcphailb/lightup.pdf
3. Batenburg KJ (2003) An evolutionary algorithm for discrete tomog-raphy. Master thesis in computer science, University of Leiden, The Netherlands
4. Batenburg KJ, Kosters WA (2004) A discrete tomography approach to Japanese puzzles. Proceedings of BNAIC, pp 243–250 5. Tsang EPK (1993) Foundations of constraint satisfaction.
Acad-emic Press, London
6. Wiggers WA (2004) A comparison of a genetic algorithm and a depth first search algorithm applied to Japanese nonograms. Twente student conference on IT, Jun 2004
7. http://www.pro.or.jp/~fuji/java/puzzle/nonogram/knowhow.html 8. Database of Japanese puzzles. http://hattori.m78.com/puzzle/
picture/java/stage_01/index.html and http://www.books.com.tw/ exep/prod/booksfile.php?item=0010317755
Chiung-Hsueh Yu was born in Hsinchu, Taiwan, Republic of China on February 26, 1982. She received the B.S. degree and M.S. degree in Computer Science from National Chiao Tung University, Hsinchu, Taiwan in 2004 and 2007, respec-tively. Her research interests in-clude image processing and algo-rithm. Currently, she is an engineer in Generalplus Technology Inc. in Hsinchu, Taiwan. She takes charge of maintaining and writing the algo-rithm of image processing and sig-nal processing.
Hui-Lung Lee was born in Taoyuan, Taiwan, ROC, on June 16, 1976. He received the M.S. degree in Com-puter Science and Information En-gineering from Fu Jen Catholic Uni-versity in 2003. Currently, he is a Ph.D. candidate of the Department of Computer Science at National Chiao Tung University. His major research interests include informa-tion hiding and watermarking.
Ling-Hwei Chen was born in Changhua, Taiwan, in 1954. She re-ceived the B.S. degree in Mathe-matics and the M.S. degree in Ap-plied Mathematics from National Tsing Hua University, Hsinchu, Tai-wan in 1975 and 1977, respectively, and the Ph.D. degree in Computer Engineering from National Chiao Tung University, Hsinchu, Taiwan in 1987.
From August 1977 to April 1979 she worked as a research assistant in the Chung-Shan Institute of Sci-ence and Technology, Taoyan, Taiwan, From May 1979 to February 1981 she worked as a research associate in the Electronic Research and Service Organization, Industry Technology Research Institute, Hsinchu, Taiwan. From March 1981 to August 1983 she worked as an engineer in the Institute of Information Industry, Taipei, Taiwan. She is now a Professor of the Department of Computer Science at the National Chiao Tung University. Her current research interests in-clude image processing, pattern recognition, Multimedia compression, content-based retrieval and Multimedia Steganography.