I am solving the N queen problem with iteration (no recursion). Problem I am facing right now is duplicate solutions. For example 4 x 4 board has 2 solutions I am printing 4 solutions, so to say I am finding same solution twice.
Let me get into the code for a better overview:
def solution(self):
queen_on_board = 0
for row in range(self.N):
for col in range(self.N):
self.board[row][col] = 'Q'
queen_on_board = queen_on_board + 1
print ("(row,col) : ", row, col)
squares_list = self.get_posible_safe_squares(row,col)
for square in squares_list:
for x,y in square.items():
if self.isTheQueenSafe(x,y):
self.board[x][y] = 'Q'
queen_on_board = queen_on_board + 1
print ("Queen on board", queen_on_board)
if queen_on_board == 4:
self.print_the_board()
self.reset_the_board()
queen_on_board = 0
so as you can see I am going over every rows and cols. This particular implementation gives me 4 solution 2 being identical.
(row,col) : 0 1
Queen on board 4
['.', 'Q', '.', '.']
['.', '.', '.', 'Q']
['Q', '.', '.', '.']
['.', '.', 'Q', '.']
(row,col) : 0 2
Queen on board 4
['.', '.', 'Q', '.']
['Q', '.', '.', '.']
['.', '.', '.', 'Q']
['.', 'Q', '.', '.']
(row,col) : 1 0
Queen on board 4
['.', '.', 'Q', '.']
['Q', '.', '.', '.']
['.', '.', '.', 'Q']
['.', 'Q', '.', '.']
(row,col) : 2 0
Queen on board 4
['.', 'Q', '.', '.']
['.', '.', '.', 'Q']
['Q', '.', '.', '.']
['.', '.', 'Q', '.']
I want to avoid getting duplicates. Would be great if someone can point me to the right direction.
The get_posible_safe_squares() method looks for possible squares in the board where the queen might be safe.
def get_posible_safe_squares(self, row, col):
ret = []
for i in range(self.N):
for j in range(self.N):
if i != row and j !=col:
if i + j != row + col and i - j != row - col:
d = { i:j }
ret.append(d)
return ret