I am trying to create my own normal 9x9 sudoku puzzle.
I divided the problem into two parts -
- creating a fully filled sudoku, and
- removing unnecessary numbers from the grid
Right now, I am stuck with the first part.
This is the algorithm I use in brief:
a) first of all I choose a number (say 1), generate a random cell position, and place it there if
- the cell is not already occupied, and
- if the row does not already have the number, and
- if the column does not already have the number, and
- if the 3x3 box does not already have the number
b) now I check for a situation in which in a row, or a column or a box, only one place is empty and I fill that
c) I check that if there is a number that in not present in a box but is present in the boxes in the same row and the same column (i am talking about 3x3 boxes here), the number's place is fixed and I fill it.
d) I repeat the above steps until every number appears nine times on the grid.
The problem I am facing is that, more than often I am getting an intermediate situation like this:
0 1 0 | 0 0 3 | 0[4/2]0
0 [2] 0 | 0 [4] 1 | 3 0 0
3 0 [4]|[2] 0 0 | 0 0 1
---------+---------+---------
2 0 3 | 0 5 4 | 0 1 0
0 0 1 | 3 0 2 |[4] 0 0
0 4 0 | 0 1 0 |[2] 3 0
---------+---------+---------
1 0 2 | 0 3 0 | 0 0 [4]
4 3 0 | 1 0 0 | 0 0 [2]
5 0 0 | 4 2 0 | 1 0 3
See the place with [4/2] written? that is the place of 2 as well as 4 because of the boxes marked [].
What can I do to avoid getting in this situation (because this situation is a deadlock - I cannot move further)