Reputation: 3
def return_solved_board(board):
solution = board.copy()
def solve(board):
for y in range(9):
for x in range(9):
if solution[y][x] == 0:
for number in range(1,10):
if check_rules_at_point(x, y, number, solution):
solution[y][x] = number
solve(solution)
#Where backtracking comes into play, if the solve method fails on the next cell than it resets current one.
solution[y][x] = 0
#Breaks out of recursion to try a differant number in cell prior
return
#Stores a solved copy of the solution into global variable
global returned_information
returned_information = solution.copy()
print(returned_information) #1st print statement
if not check_for_valid_board(board):
return "This board is not possible to solve."
else:
solve(board)
global returned_information
print(returned_information) #2nd print statement
return returned_information
print(return_solved_board(valid_sudoku_board1))
I am trying to write a program to solve sudoku puzzles. The puzzle is stored in a list of lists. When I run this program the first print statement returns the correct solved list of lists. However, the second return statement returns the original unsolved puzzle. Both of their id's are the same however I can't figure out why returned_information in the second print statement changes back as returned_information is only called once.
Upvotes: 0
Views: 165
Reputation: 169032
You're probably bumping into dicts'/lists' copy()
being a shallow copy; use copy.deepcopy(...)
instead to do a deep copy.
Upvotes: 1