Backtrack through N queens
Backtracking builds a solution one choice at a time and undoes a choice as soon as it cannot lead anywhere. For N queens, place one queen per row and reject any column or diagonal already under attack.
cols, diag1, diag2 = set(), set(), set()
row, col = 2, 1
print(row - col, row + col) # 1 3Think before coding
Cells on one diagonal share row - col; cells on the other share row + col. Three sets make each safety check constant time. After exploring a placement, remove it from the sets so the next column starts clean. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write solve(n) returning every solution as a list of column indices, one per row, in lexicographic order.
The explanation opens here once every check passes.