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))
我想写一个解数独谜题的程序。谜题被存储在一个列表中。当我运行这个程序时,第一条print语句返回正确的解题列表。然而,第二条返回语句返回的是原来的未解谜题。它们的id都是一样的,但是我不明白为什么第二条打印语句中的return_information会变回来,因为return_information只被调用了一次。
你可能会碰到dicts'list'。copy()
浅尝辄止 copy.deepcopy(...)
而不是去做深度复制。