I wonder if this is a good way to write a fast sudoku solver? I made one that worked very quickly which used first of all human style technique for trying to solve the puzzle (before trying brute force).
I set up 9 bit grids for each of the nine numbers and searched for places in which only one possible number could go. Here's a quick drawing to explain:
Puzzle:
200 000 060
000 075 030
048 090 100
000 300 000
300 010 009
000 008 000
001 020 570
080 730 000
090 000 004 |
And here is what the 1s bit grid (bool bg1[9][9]) would look like (0 = empty cell, 1 = a 1 can't go here):
101 010 111
001 011 111
111 111 111
001 111 100
111 111 111
001 111 100
111 111 111
111 110 100
111 010 101 |
Then you check for any 9 horizontal, vertical or 3x3 box with only one gap on each of the 9 bit grids. If you find a horizontal, vertical or 3x3 with just one gap, then that number must go in there. This method is very very fast for solving human solvable grids.
Also, when it comes to brute force, you can use the bit grid to substantially reduce candidate numbers for a cell. For example in cell no.2 on the top row, you would only need to try 3,5,7,9 as the others would be marked as unable to be occupied by the other numbers on the bit grids.
In fact, on the 8s bit grid, you can see an example of this:
111 001 010
111 011 010
111 011 100
011 111 000
111 111 001
111 111 111
111 011 110
111 111 111
111 001 001 |
An eight must belong in the 1st column, 4th row down. Updated 8s bit grid:
111 001 010
111 011 010
111 011 100
111 111 111
111 111 001
111 111 111
111 011 110
111 111 111
111 001 001 |