def answer_solve_sudoku(__grid):
res = check_sudoku(__grid)
if res is None or res is False:
return res
grid = copy.deepcopy(__grid)
# find the first 0 element and change it to each of 1..9,
# recursively calling this function on the result
for row in xrange(9):
for col in xrange(9):
if grid[row][col] == 0:
for n in xrange(1, 10):
grid[row][col] = n
new = answer_solve_sudoku(grid)
if new is not False:
return new
# backtrack
return False
# if we get here, we found no zeros and so we're finished
return grid
Here is the code, and check_sudoku(grid)
can return if a grid is a valid sudoku or not.
I just can't understand the recursion part, I tried to write down the process on the paper, but it failed everytime, how is backtraking working? and what is new
? if the answer_solve_sudoku(grid)
is valid?
I know it sets every 0 to 1..9, and checks if it's a valid grid or not, but I just can't draw the whole process on the paper. And can't really understand how the backtrack is working.
btw, is there any advice of understanding recursion code?
Best Regards,
Sheng Yun
EDIT
I read the code again and again, and now I have some understanding, but I'm just not that sure about this, it will be kind if anyone gives me some comments.
1, return new
will only be called when the solver found the solution, and this will be called right after return grid
2, when will
# backtrack
return False
be called? if the next solution isn't right, check_sudoku(__grid)
will return False
, and if the next solution is right, it will call another answer_solve_sudoku(grid)
till it gets the right solution, and when it gets the right solution, it will return grid
and then return new
. So when is:
# backtrack
return False
called?