Attempt to create a Sudoku solver in python
Posted: Αυγούστου 11th, 2011 | Author: Giorgos | Filed under: programming | Tags: python, sudoku solver | 1 Comment »I will try to make a Sudoku solver in python as a summer project. Hope to finish it.
I will not explain the rules here. You can check them in wikipedia’s article for sudoku or summarized here.
First I must find a way to represent Sudokus in python. I am thinking to store each puzzle in a file formatted like:
5 3 x x 7 x x x x
6 x x 1 9 5 x x x
x 9 8 x x x x 6 x
8 x x x 6 x x x 3
4 x x 8 x 3 x x 1
7 x x x 2 x x x 6
x 6 x x x x 2 8 x
x x x 4 1 9 x x 5
x x x x 8 x x 7 9
Then I will create an object named Position which will hold each cells information. For example :
object Position
{
row
column
box
fixed
value
}
So number 5 in the first row is an object with the following properties:
row = 0
column = 0
box = 0
fixed = true
value = 5
The Sudoku puzzle has 9×9 = 81 Positions.
Each column, each row, and each of the nine 9 boxes must contain all of the digits from 1 to 9.
Now we have to find an algorithm to solve the puzzle. That’s the most difficult part
.
The first it comes to my mind is to use a Brute Force algorithm without any logic. It will provide
a solution in every case but I think it will be slow. Moreover I would like to implement some logic.
Another possibility would be to fill randomly all the empty cells, counting the errors and trying to eliminate them by
changing the cells values. I like this better.
The problem is the random. If I find a way to fill the cells with a clever way the changes will be fewer and the algorithm will be
more efficient.
I am not sure that it will always provide a solution but I will give it a try.
I may also find useful that the sum of each column,row,box will be 1+2+…+9=45.
That’s in theory now I will have to write some code.
To be Continued …..
Tweet
[...] Continue from here. [...]