Simple Sudoku puzzles can be solved without any recursion. You go through each empty block and try to solve it. If it is the only block in
- The row
- or the column
- or the block
Then you enter the number. You do this until your riddle is solved.
Recursion kicks in!
However, if you stagnate and can't solve any more empty fields with this method, you have to go recursive. Pick the block that has the fewest possible options, for example a block where only 3 numbers are possible. Perform a recursion on those 3 numbers, trying each one. Then, try again by the method described above. When you stagnate again, make another step in recursion.
Many of your recursion cases will end up in a Sudoku riddle that is wrong (i.e. not solvable). You just ignore them. One of these cases will solve it.
Pseudo code
I'll try to mediate the idea here, not just paste a bunch of code. You should be able to solve this. It's a brain teaser, but possible
function Solve(grid)
while
SolveByLogic(grid)
if (grid is not solved)
cell = find cell with the least possible options
for each of this option
try to => Solve(grid)
next
end
wend
end
function SolveByLogic(grid)
// Try to solve it by pure logic
while
foundAny = false
for each empty cell
if (cell is the only empty cell in row)
cell = what's left in that row
foundAny = true
else if (cell is the only empty cell in column)
cell = what's left in that column
foundAny = true
else if (cell is the only empty cell in grid)
cell = what's left in that grid (by grid I mean the 3x3 grid)
foundAny = true
next
if (foundAny == false) return //No more logic solving possible
wend
end
Note: This is not a complete algorithm that you can just convert into your language and use! But it should give you the idea that you need in order to solve this!
Good luck :)