Play5sDeveloper notes

A Nonogram Line Solver With Dynamic Programming

The Play5s Nonogram runs every puzzle through a line solver that asks what all legal placements of one row or column have in common, and a puzzle is only used if that logic alone can finish it. The solver is a dynamic-programming table with one entry for each group and position, and it feeds three things: the puzzle builder, the Hint button and the greying of clue numbers that are already satisfied.

This note covers the line-solving logic of the Nonogram page, not the painting controls.

What does solving a single line mean?

Clue 3 2 on a line of seven cells: every legal placement1234567placement Aplacement Bplacement Ccertain????Filled in allthree: 2, 3, 6The solver keeps, for every cell, whether it can be blank, filled, or both.
Clue 3 2 on seven cells1234567ABC????certainCells 2, 3 and 6 are filled in all threeplacements, so they are certain.A, B, C: the legal placements.
Clue 3 2 on a line of seven cells has three valid placements. The cells they all fill, two, three and six, are certain. The other cells stay unknown.

A nonogram clue such as 3 2 says that the line holds a run of three filled cells and then a run of two, in that order, with at least one blank between them. Wikipedia's article on nonograms describes the puzzle and says a properly constructed one has only one way to fit all the runs. Solving a line is a smaller problem than solving the puzzle: given what the player already knows about each cell, find every cell whose state is the same in all legal placements.

Take seven cells and the clue 3 2. The shortest line that can hold it needs six cells, so there is one cell of slack. The three runs can sit in three ways, as the figure shows. Cells two, three and six are filled in every one of them, so they are certain. The first cell is filled in two placements and blank in one, so it stays unknown. That is the whole idea, and it is the overlap trick that experienced solvers do by eye, done exactly.

The solver keeps a mask for each cell. Bit 1 means the cell can be blank, bit 2 means it can be filled, and 3 means unknown. The output mask of a line is the union of the cell values over all legal placements. A cell whose union is a single bit is solved.

Why a table instead of trying every placement?

A 20-cell line with five runs has many placements, and a puzzle asks for the same answer over and over as the rows and columns inform each other. solveLine avoids the explosion with a table, in the spirit of dynamic programming. The question "can the groups from index g on be placed in the cells from position pos on?" depends only on those two numbers, so the answer is stored the first time it is computed.

const dp = (pos, g) => {
  if (pos === n) return g === k;
  if (n - pos < minNeed[g]) return false;      // not enough room left
  const cached = memo[g * stride + pos];
  if (cached !== 0) return cached === 2;
  let ok = false;
  // blank at pos?
  if ((inMask[pos] & 1) && dp(pos + 1, g)) { outMask[pos] |= 1; ok = true; }
  // start group g at pos? every cell must allow filled, the next must allow blank
  if (g < k && fits(pos, clues[g])) {
    if (dp(next, g + 1)) { mark filled cells and the separator; ok = true; }
  }
  memo[g * stride + pos] = ok ? 2 : 1;
  return ok;
};

The version in the source is a little longer, but this is its shape. Two branches at each step: put a blank at the current position, or start the next run there. A run may start only if every cell it covers can be filled and the cell after it can be blank, unless it is the last run. A suffix-sum table, minNeed, rejects branches early when the remaining cells cannot hold the remaining runs. The memo is a typed array of (groups + 1) times (cells + 1) entries, so the work grows with the line length times the number of runs, not with the number of placements.

The table also gives the union for free. Whenever a branch succeeds, it sets the bits for the cells it chose in outMask. A later visit to the same state returns the cached answer without marking again, which is safe because the first visit already marked every cell reachable from that state. The result is the exact union over all placements. It is never a guess, and it never misses a placement.

How do rows and columns help each other?

solveByLineLogic runs the line solver on every row, writes what it learns into the board, and marks the crossing columns as dirty. Then it does the columns, and marks rows dirty for any cell that changed. It repeats until a full pass changes nothing. A line that is not dirty is skipped, so a solved region costs nothing. If any line has no legal placement at all, the function reports a contradiction. If the pass ends with every cell decided, the puzzle is solved, and the function also reports how many passes it took.

The pass count matters for one reason: a puzzle that falls to a single sweep is boring, so on the 10, 15 and 20 sizes the builder asks for at least two passes, and gives up the requirement only after many attempts.

The same function guarantees fairness. A puzzle solved by line logic alone has no point where the player must guess, and a line-solvable grid has exactly one solution, since each step was forced. That guarantee, not a hand check, is how a Play5s Nonogram is accepted, whatever the size from 5 by 5 up to 20 by 20.

How does the builder keep puzzles solvable?

The picture comes first. The builder draws a candidate with symmetric stamps and some smoothing, computes its clues, and hands them to solveByLineLogic. Sometimes cells remain undecided because a small block of cells can be swapped without changing any clue, the classic ambiguous 2 by 2 pattern. The builder then flips one undecided cell and tries again. The number of flips is limited by size, 6, 12, 18 and 26 for the four sizes, and a candidate that keeps failing is thrown away. The builder will try up to 120 candidates.

A well-formed check rejects the rest. Every row and column must contain a filled cell, and the picture must cover between 36 and 78 percent of the area, so a grid is never a few lonely dots or a solid block. The checks are plain functions, with no page code in them. Anything that looks like a picture and passes them can be played.

How do hints and crossed-off clues use the same solver?

The Hint button (hint) first looks for mistakes: a filled cell where the solution is blank, or a cross where it is filled. Then it runs solveLine on every row and column with the player's own marks as input and keeps the first cell that the line proves. A cell the solver proves filled comes before a cell it proves blank. The message names the line, for instance "Row 4 forces row 4, column 6 to be filled." The player sees which row or column to study, not only a square to press.

lineClueStatus compares the runs the player has made with the clue and greys out the clue numbers that are satisfied. It scans from the left and from the right, crossing off a clue only when a run of that length is closed by a border or a cross mark. A line with a longer run than any clue, or more filled cells than the clue total, is flagged as over. The same logic is written as plain functions, so any size of puzzle gets it. The row-by-row reasoning here is the cousin of the technique ladder in the Sudoku note.