User:Italick/SudokuSolutionEnumeration

To count the number of finished sudoku grids:

Find the 280 ways to regroup a group of 9 different things into 3 new groups each having 3 of the 9 things.

Make a lookup table with a list of ways that the top right corner box (3x3 section) can be solved, given the 2 neighboring boxes:


 * The neighboring box to the left is filled with fixed contents (your choice).
 * The columns of the neighboring box below are sorted in a standard order. There are 280 ways to populate the columns in this case, so the table has 280 lists of solutions to the corner -- one list for each way to populate the columns below the corner box and ensure that they are sorted.

It helps to have a way to encode the contents of a set of rows without capturing the precise positions of the digits within each row. Allocate 9 bits to each row in a 27-bit number. In every series of 9 bits that is allocated to one row, each bit confirms the presence or absence of a certain digit in the row. Of course, this encoding technique works for sets of columns.

Here is an example of an algorithm to determine the "standard" way that rows and columns of a box should be sorted during the table generation:


 * The row (or column) with a '9' in it must be first.
 * The row (or column) with the greatest digit that is not in the first row (or column) must be second.
 * The row (or column) that is not first or second must be third.

A typical number of corner solutions, given a pair of filled neighboring boxes is 400, and it varies with the pair of neighboring boxes. The number of solutions to a corner box cannot exceed 448 when the state of its neighbors is specified.

Make a lookup table that lists the 8 or zero ways to solve the top left corner box, given each combination of the 1680 ways that its own rows could be populated (without sorting the rows to the standard sequence) and the 280 ways that the columns of the neighboring box below it could be populated and sorted in standard order. This table also serves as a table of lists of ways to solve the lower right corner box given any combination of one of the 1680 ways to populate its columns and one of the 280 ways to populate and sort the rows of its neighbor to the left.

Make a Boolean lookup table that answers the question of whether each combination of one of the 280 ways to populate and sort rows with one of the 280 ways to populate and sort columns could together specify the way that the rows and the columns are constituted, in a single box holding a permutation of the digits 1 through 9.

Write a program that picks each possible way to populate and sort the columns of the non-corner boxes that are below corner boxes -- and populate and sort the rows of the box below the center box -- and pick the solution in the top right corner -- and pick the solutions in each of the top left and bottom right corners. The box above the center box is assigned the same fixed contents that it had during the generation of a lookup table earlier and the center box is being ignored. Every time the program assembles a state for all of the boxes except the center and lower left corner boxes, it should consult the Boolean lookup table for the answer to whether the lower left corner could have a solution. Every time the program receives "yes" from the Boolean table, it should increment a counter. Every time the program switches the state of the non-corner boxes -- a routine that it performs only after it has assembled and tested every combination that includes the state of the non-corner boxes being changed -- the program should write the number of found solutions to another lookup table and reset the counter. The lookup table being generated by this section of the program should be a 3 dimensional array (280x280x280) that reports the number of solutions, given the state of the non-corner boxes that was varied throughout the course of the loop, as well as the fixed content of the box above the center box. If all of a sudoku puzzle is solved randomly except the corner boxes which remain blank, the average number of ways to solve all four corners is near 2635. (I did not precisely compute the average.)

For the next loop, fix the contents of the center box. For each of the (2016*36)^2 ways to fill all 4 boxes around the center box so that the columns of the boxes to the left and right of the center are sorted in standard order, and the rows of the boxes above and below the center are sorted in standard order, increment a counter by the number of solutions for this state. To do find the number of solutions, the program needs to consult the lookup table made before this stage. This loop must use a renumbering transformation. When the loop has picked each one of the (2016*36)^2 states, it produces a grid that conforms with this state. Then it renumbers the grid so that the box above the center box has the contents that it had during the generation of the lookup table, and it consults the lookup table after the renumbering transformation.

When I run my own program like that, the counter reaches the number of finished sudoku solutions divided by 9!*6^4. Multiplying the counter by 9!*6^4 accounts for the states that were not tested (different ways to fill the center box, and unsorted rows or columns in the neighbor boxes).

The speed of my program could benefit from further reworking to remove redundant loop cycles. When everything is solved except the corner boxes, the solved boxes on the left and right could be swapped and there would be the same number of ways to solve the corners. I did not attempt to remove that source of redundancy and the program still finishes on my computer after about 8 hours.

Good luck.