I was going through this really neat solution for n-queen problem from Elements of Programming Interviews, Recursion chapter, but can't seem to understand a particular piece of code. If someone can explain the logic here, it would be really helpful. If condition for checking conflicts is something I am trying to wrap my head around here but to no success.
def n_queens(n: int) -> List[List[int]]:
def solve_n_queens(row):
if row == n:
# All queens are legally placed.
result.append(col_placement.copy())
return
for col in range(n):
# Test if a newly place queen will conflict any earlier queens place before
# I am struggling to make sense of this if condition
if all(abs(c - col) not in (0, row - i)
for i, c in enumerate(col_placement[:row])):
col_placement[row] = col
solve_n_queens(row + 1)
result: List[List[int]] = []
col_placement = [0] * n
solve_n_queens(0)
return result