I'm doing eight queens in python. I want to put all the outputs in a list by using the append method. However, it's very weird that all the outputs become the same. Can anyone help me plz ;)
I'm testing my program using four queens:
board = []
solution=[]
def isConflict(x, y):
for (i, j) in board:
if x == i:
return True
if y == j:
return True
if abs(x - i) == abs(y - j):
return True
else:
return False
def solve(x,y):
for y in range(1, 5):
if isConflict(x, y)==False:
board.append((x, y))
solve(x + 1,y)
board.remove((x,y))
if x > 4:
solution.append(board)
print(solution)
solve(1,1)
The output is like:
[[(1, 2), (2, 4), (3, 1), (4, 3)]]
[[(1, 3), (2, 1), (3, 4), (4, 2)], [(1, 3), (2, 1), (3, 4), (4, 2)]]
However what I want is:
[[(1, 2), (2, 4), (3, 1), (4, 3)]]
[[(1, 2), (2, 4), (3, 1), (4, 3)], [(1, 3), (2, 1), (3, 4), (4, 2)]]
What shall I do?