Les Hammer
2005-11-27 21:10:51 UTC
I have written a vi that solves the Sudoku number puzzle. I'm looking for
suggestions for improvement and/or Sudoku fans who have hard to solve
puzzles to check out the operation.
To use the vi fill in the known values, leavingthe 0's in the places where the puzzle has blanks. Click the run arrow to findthe solution.
Theory of operation:
Each cell is given a hexidecimal code. If the cell value is known then the codeis simply the U16 for that number (0x0001 to 0x0009). If unknown (initially 0in the array) it is coded with 0x7FC0 - one bit for each possible value, with0x0040 representing that the cell could be a "1", 0x0080 representing "2",... through 0x4000 representing that the cell could be a "9."
First the array is scanned. If a single bit (with a value greater than 9) is foundin a cell, then that bit is removed as a possibility for all other cells in that row,column and 3x3 group of cells. The cell with the single bit is then recoded withthe value 1 through 9. The array is then rescanned to see if any more cells havehad all but one bit removed.
If no more cells with single bits are found, the array is searched for a row, column or 3x3 group of cells in which two cells have the same hexidecimalvalue of only 2 bits. Since those 2 bits must be in one or the other of thosetwo cells, those bits are removed as possibilities for other cells in that row,column or 3x3 group of cells. Then it goes back to the step of looking forsingle bits.
If no more single bits or two of 2 bit values are found, the vi then goes into aguessing mode. The 9x9 array of values is saved. A cell with more than onebit is found. One of the bits is selected, and the program continues with the"looking for single bits" mode. If this track leads to a 0 in a cell the guess was bad. The program goes back to the saved array and guesses the nextbit. If this leads to again having no more single bits or two of 2 bit values found, then a second level of array saving is done and another cell is selectedfor the guessing game. The process is continued until all cells have values inthe 1 - 9 range.
Les HammerSmart Sensor Systems<a href="mailto:***@SmartSensorSystems.com" target="_blank">***@SmartSensorSystems.com</a>
Sudoku.vi:
http://forums.ni.com/attachments/ni/170/153770/1/Sudoku.vi
suggestions for improvement and/or Sudoku fans who have hard to solve
puzzles to check out the operation.
To use the vi fill in the known values, leavingthe 0's in the places where the puzzle has blanks. Click the run arrow to findthe solution.
Theory of operation:
Each cell is given a hexidecimal code. If the cell value is known then the codeis simply the U16 for that number (0x0001 to 0x0009). If unknown (initially 0in the array) it is coded with 0x7FC0 - one bit for each possible value, with0x0040 representing that the cell could be a "1", 0x0080 representing "2",... through 0x4000 representing that the cell could be a "9."
First the array is scanned. If a single bit (with a value greater than 9) is foundin a cell, then that bit is removed as a possibility for all other cells in that row,column and 3x3 group of cells. The cell with the single bit is then recoded withthe value 1 through 9. The array is then rescanned to see if any more cells havehad all but one bit removed.
If no more cells with single bits are found, the array is searched for a row, column or 3x3 group of cells in which two cells have the same hexidecimalvalue of only 2 bits. Since those 2 bits must be in one or the other of thosetwo cells, those bits are removed as possibilities for other cells in that row,column or 3x3 group of cells. Then it goes back to the step of looking forsingle bits.
If no more single bits or two of 2 bit values are found, the vi then goes into aguessing mode. The 9x9 array of values is saved. A cell with more than onebit is found. One of the bits is selected, and the program continues with the"looking for single bits" mode. If this track leads to a 0 in a cell the guess was bad. The program goes back to the saved array and guesses the nextbit. If this leads to again having no more single bits or two of 2 bit values found, then a second level of array saving is done and another cell is selectedfor the guessing game. The process is continued until all cells have values inthe 1 - 9 range.
Les HammerSmart Sensor Systems<a href="mailto:***@SmartSensorSystems.com" target="_blank">***@SmartSensorSystems.com</a>
Sudoku.vi:
http://forums.ni.com/attachments/ni/170/153770/1/Sudoku.vi