I am thoroughly addicted to puzzles. Sudokus, collaborative exit room board games, Wordle, the Knotwords app, jigsaw puzzles. I always have a puzzle book on me or an app ready for the train ride. As I am writing this I am looking at a stack of board games waiting to be played sitting next to me on my shelf. My absolute favorite kind of puzzle is the Nonogram (or Hanjie, Oekaki Logic, Paint by Numbers, Picross, Griddlers…). You might have seen one before.
The rules are fairly simple. You get a grid. Every row and column has a list of numbers (or hints). You want to color in the right set of cells and the hints tell you which. A single number in a row (or column) means you have to color in that many cells consecutively in that row (or column). A way to think about this is to imagine that you have the solution and you are walking along one of the horizontal lines from left to right and lets say the hint attached to the line (on your imaginary left) is“5 2”. While walking eastward, if you keep looking to your left you see a number of cells. At some point one of them will be colored in. You start counting how many cells are colored in before you see a blank cell again. One, two, three, four, five, blank! Five colored cells in a row. If you check the list of hints and you can see that matches the first number. So you keep walking and the next time you see a colored cell you start counting again. This time it should be two to match the second number. If this is correct for all horizontal and vertical lines (on both sides), the Nonogram is solved and you’ll have a picture in the grid as the result. Here’s an example.
Cool right? A bit pixelated though. Works well for an 8-bit retro image, but what if we want a solution with curves? Let me show you the Curved Nonogram:
Alright, so what’s going on here? It’s not a grid anymore, but we still have various cells and the goal is still to color them in according to some hints. The hints look a bit different here. On the ends of every curve there are small boxes attached to it. They sort of look like little flags and there’s one set of these flags attached to either side of the curve. Now you can again walk along one side of a curve and the counting rule is the same. The blocks of colored cells on one side of a curve should correspond to the hints (flags) on that side of the curve.
Here’s what I love about curved Nonogram. What do you get when you solve a Sudoku. Well, a solved Sudoku, 81 numbers on a piece of paper. For a crossword? Maybe a word or a phrase. But for a curved Nonogram? You get an entire picture. And not just the pixel version. It might not sound like much but, as I said at the start, they are surprisingly addictive. So this is the point where you might expect me to link to a cool website with more examples, recommend you an app that has all kinds of different curved Nonograms or maybe recommend you my favorite puzzle book on the topic. And I’d love to, but there’s a slight hitch. There aren’t any. I only really know of them because a friend of mine is in the habit of creating holiday themed puzzles as greeting cards and I got one for Christmas.
Let's create a Nonogram!
So maybe let’s try to create one together. How hard can it be? Let’s start with an image, that we would like to have as a solution. Something simple, like this picture of a crescent moon below. Then we extend some of the curves, add some additional ones to make the solution less obvious and put the entire thing in a box (middle). I also already placed some of the clues in the figure on the right (I left out the ones that would just say ‘0’). Now only the red curve has to be labeled. So lets do it together. We start at the bottom and walk along it looking to our right. We see a blank cell, then a colored in cell, then blank again, filled in and finally blank again. So by our rules, we would place two clues which both say ‘1’.
But wait a minute, we have not actually seen two colored in cells, it was one and the same. So, do we maybe only place one clue? And if so, should it say ‘2’ then? This is at best unclear and at worst very confusing for someone trying to solve the puzzle.
To explain what’s happening we need some terminology (don’t worry this is not where it’s getting technical… At least not yet!). The curves we placed partition the square into cells. Every cell has a boundary which consists of parts of the different curves which form the border of the cell. You can think of the boundary like a fence around the cell. And the problem we have just seen stems from the fact that we have a curve (the red one), which appears more than once on such a boundary of a cell. We call such a cell a bad cell (not very creative I know, but at least it’s easy to remember).
The gray cell is an example of a bad cell.
So we do not want to have any bad cells in our Nonograms. And it turns out that this is a common problem. There is an automated methods to generate curved Nonograms. A large number (about 90%) of puzzles this system creates contain bad cells. So what can we do about that? Well we could move some of the curves but we really don’t want to mess with the figure. So here’s an idea: If one curve appears twice on the boundary of a cell we can try to draw a new curve through the cell to separate the two parts.
This leads to the following question: Can we just keep adding new curves to make it better? Let’s look at the example again and let’s make it simple. Can you add a single curve so we don’t have any of the bad cells? It’s like a puzzle in a puzzle. A solution looks like this:
For the small puzzle it kind of works to figure out a solution by hand. (the green curve in the picture above). But what if we go bigger? Look at this version of the same puzzle with more additional curves.
It doesn't even have that many more curves, but it is becoming already very tricky to even find all bad cells, let alone checking if there’s a single additional curve which properly cuts through all of them.
Let me make it a bit easier. Here’s a version where I connected all curve parts that appear multiple times.
Now it’s a bit clearer and we can reformulate our question: can we add a new curve, which cuts through all of the red connections? To make sure that this is the same as cutting all bad cells into two good ones, there is one more thing the curve cannot do. It cannot go through a cell twice (it would made the cell bad itself).
At this point I would like to go on what might appear to be a little tangent, but I promise it becomes relevant later. Let’s look at the solution to the small version again. Or more precisely: at a couple:
These all look different, but if we break it down they all go through the same list of cells in the same order. And we can try to represent curves added to a puzzle in a way that only uses this information. And we will use Graphs or Networks for this (Hey, that’s in the website name!).
Image putting a dot into each cell. Then connect cells if they are neighbors. For technical reasons the outside of the puzzle counts as one big cell here, but I will ignore that for now. The result is a network called the dual graph of the puzzle and it looks something like this.
A big loop in this dual graph (usually called a cycle) now represents the order in which any new curve moves from one cell to the next.
OK, that’s a nice tangent and all, but how does it help us? Well let's use this idea of representing a solution as a big loop in the dual graph and state the original problem in that language. We want to find a cycle in the dual graph of the puzzle that visits all bad cells and cuts all red connections (remember that this was the same as a curve that cuts all bad cells into two new good ones).
And wouldn’t you know it, we have successfully taken our puzzle creation problem and have turned it into a question about graphs. For our problem it turns out that we can use a result of Björklund, Husfeldt and Taslaman, who have shown that one can find* a cycle in the dual graph of our puzzle** efficiently*** with a very clever algorithm.
As you can maybe tell by all the asterisks attached to the previous sentence, I might have skipped some details there. If you want to see the nitty-gritty let’s get into it, but fair warning: beyond here there be jargon. If you want to save that for some other time and would like to skip to the end instead, the technobabble stops at the start of the last paragraph.
So it seems we can figure out (under some assumptions) how to solve our problem with a single curve. Where to go from here? Well, the natural extension is to use more than a single curve. If you checked out the technical details, you saw an example of a puzzle where we can not take care of all bad cells with a single curve. Can you find a similar counterexample of a puzzle which cannot be solved using two curves? And with that, have fun solving puzzles! Should you stumble over a curved Nonogram in the wild, let me know!