Write a sudoku solver.
Anonimo
---- DISCLAIMER: I've written this in 10 minutes, it might have some flaws, but you should be able to get a general idea for a very basic Sudoku solver (there are some more advanced methods for eliminating possibilities which I can't remember now). ---- General idea: Like any veteran Sudoku solver who marks the possibilities for empty cells, we will do just that and record the possible values ("possibilites") for every cell. the initial, or given, cells have obviously just one, while the rest we set as having all 9 possible values. For every cell newly found (starting with the "given" ones) we eliminate that possible value from the row, column and 3x3 square associated with that cell. If the Sudoku puzzle is well-formed we should get to an answer in not-so-many steps/loops. ---- Data * table[][] ** bool solved = true IF the cell has a known solution (applies to the initial ones as well) ** bool possibilities[1..9] = TRUE if the cell might be the respective value (1-9) ** uint pCount = number of possibilities (initially 9, lastly 1) * removePossibilities(uint x, uint y) *: if (x,y) is known it deletes the cell's value from the associated ROW, COLUMN and 3x3 TABLE * findNewlySolvedCells *: returns a list of coordinates for every cell with solved = 0 and possCount = 1 * isSolved() *: end loop condition - returns zero if there are still unsolved cells * unsolvedCellsCount - initially 81, lastly 0 ---- Initializations: * solved = 0 * poss[1..9] = 1 EXCEPT FOR GIVEN CELL VALUES which have poss[i] = 1, the rest 0 * possCount = 9 EXCEPT FOR GIVEN CELL VALUES which have possCount = 1 * unsolvedCellsCount = 81 ---- Program loop: WHILE NOT isSolved() * get list of newly solved cells with findNewlySolvedCells * unsolvedCellsCount -= size of that list * use removePossibilities for all the cells in the list * set solved = 1 for all cels in the list