Tuesday, July 28, 2009

C++ Sudoku program?

Can anyone please tell me a detailed algorithm on how to program a C++ program that solves sudoku puzzles. Any help and advice would be greatly appreciated.

C++ Sudoku program?
Here are a few:


http://www.8325.org/sudoku/source.html


http://www.kgsoft.co.uk/solver.htm


http://www.idiap.ch/~dimitrak/software/s...


Good luck on your programming ^_^
Reply:There are a couple ways to go about this.





First approach would be to find the first empty square, put a "1" in it, and see if that solves the puzzle. If not, go to the next empty square and put a one in it and repeat the above. If there are no more empty squares, go back to the last one you changed and increment it to the next value and repeat the process. Eventually you will have tried every value in every empty square and will have solved the puzzle.





That could take a long time to execute depending on the speed of the machine. (Note it wouldn't be affected by the level of difficulty of the puzzle.)





The other way to approach it would be to solve it the same way you do in real life. Look for squares that can only have one value and fill them in, repeating until there are no more. Then look for patterns in the possible values, such as two squares that can only have the same two values (you can then eliminate those values from the other squares in the same row, column, or block of nine as the two squares you found). Etc. Apply your heuristics until you can't do any more, then you have to resort to a variation on the first approach, where you recursively try all values in the remaining squares.





This would be a fun exercise, and it would be especially interesting to run two algorithms against each other and see how long they each take to solve the puzzle.


No comments:

Post a Comment