A very small Shikaku in progress. |
In Shikaku your task is to divide a grid into rectangular regions, such that every cell is assigned to a region, every region has exactly one number in it, and that number is the area of the region.
This one turned out to be harder to generate than Hidato, though maybe I haven't found the right algorithm. The first step was to generate a random division of the grid into rectangles (i.e. to generate the solution). I didn't find a good algorithm for that, so I made up a mediocre one:
- Choose a random cell that isn't yet in a rectangle
- Randomly decide on a major axis (horizontal or vertical)
- See how far we can stretch from that cell along the major axis (e.g. for horizontal we see how many empty cells there are to the left and right)
- Choose a random range from that span, with length no greater than 8
- See how far a rectangle of that width can go along the minor axis
- Choose a random range from that span, no longer than 8, where the rectangle's area is no bigger than 16
- If we were able to pick a rectangle bigger than 1x1, then go back to 1 and repeat until all cells are consumed.
- If not, then pick a random rectangle adjacent to the cell and remove it, then go back to 1.
A grid, divided. |
I wrote a brute force solver, but it was taking far too much time for puzzles of moderately-large size. One trick I used to speed it up was to do a first pass on each hint, seeing how many possible rectangles could go there at the beginning of the puzzle. Then we sort the hints from fewest to most possible rectangles, ensuring that we try all the "forced" ones first.
I suspect I can make the solver even faster if I resort at each decision in the solver tree, because placing one rectangle frequently reduces the number of options for another, and I find that for most of the Shikaku I'm generating, every rectangle is forced at some point or another, leaving the best kind of brute-force tree ever: a straight line to the solution.
There was another problem. Too often the generator would put two 2x1 rectangles next to each other, and too often the hint-picker would select kitty-corner hints from those rectangles, giving the puzzle multiple solutions.
An ambiguous Shikaku. |
That left me with a fast-enough generator and a not-so-fast-but-maybe-ok solver to verify the puzzles.
A generated Shikoku-like puzzle. |
For the other puzzle types, I have a feature that allows you to adjust the "difficulty" of the puzzle, but so far I don't have that for Shikaku. I could go by the number of choices made in the solver (which is what I did for Numbrix and Hidato), or I might even be able to use "number of initially-forced regions". However I would need to speed up the solver before I attempted that. At the highest grid-size setting, 15x15, it currently takes a while just to generate one puzzle, and if it started getting picky about difficulty it might take way too long.
Solving in progress. |
No comments:
Post a Comment