Tell us what’s happening:
The output looks right, could anyone tell me what’s wrong of my code?
Your code so far
solutions = []
visited = []
def dfs_n_queens(n):
if n < 1:
return []
dive_in(n, 0)
return solutions
def dive_in(matrix_size, row):
if row >= matrix_size:
solutions.append(visited.copy())
return
for col in range(matrix_size):
if col not in visited and all(abs(col - v) != abs(row - i) for i, v in enumerate(visited)):
visited.append(col)
dive_in(matrix_size, row + 1)
visited.pop()
print(dfs_n_queens(5))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Challenge Information:
Implement the N-Queens Problem - Implement the N-Queens Algorithm