Tell us what’s happening:
My current solution only works until the board is 4x4, after that it doesn’t give all the solutions. The problem is that it explores one possible path, but doesn’t backtrack to search for other possible branching paths. But I’m not sure how to implement backtracking with the code the way it is
Your code so far
def dfs_n_queens(n: int) -> list[list[int]]:
solutions = []
if n < 1:
return solutions
def search(row_i: int, column_i: int, solution: list[int], board: list[list[int]]) -> None:
board[row_i][column_i] = 1
for i in range(n):
for j in range(n):
# Mark row
if i == row_i and j != column_i:
board[i][j] = 2
# Mark column
if i != row_i and j == column_i:
board[i][j] = 2
# Mark diagonals
for l in range(n):
if l != 0:
if row_i+l < n and column_i+l < n:
board[row_i+l][column_i+l] = 2
if row_i+l < n and column_i-l >= 0:
board[row_i+l][column_i-l] = 2
if row_i-l >= 0 and column_i+l < n:
board[row_i-l][column_i+l] = 2
if row_i-l >= 0 and column_i-l >= 0:
board[row_i-l][column_i-l] = 2
# Print board for testing
for row in board:
print(row)
print()
queens = 0
possible_paths = 0
visited_paths = []
for i in range(n):
for j in range(n):
if board[i][j] == 1:
queens += 1
solution[i] = j
if queens == n and solution not in solutions:
solutions.append(solution)
elif board[i][j] == 0:
possible_paths += 1
while len(visited_paths) < possible_paths:
if (i, j) not in visited_paths:
search(i, j, solution, board)
visited_paths.append((i, j))
for i in range(n):
for j in range(n):
search(i, j, [0] * n, [[0] * n for _ in range(n)])
return solutions
print(dfs_n_queens(5))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:154.0) Gecko/20100101 Firefox/154.0
Challenge Information:
Implement the N-Queens Algorithm - Implement the N-Queens Algorithm