Hard · Backtracking with Sets
Understand → Visualise → Practice.
Place one queen per row. A candidate is safe only when its column and both diagonals are unused.
Cells on one descending diagonal share r − c; cells on one ascending diagonal share r + c.
Exactly one queen belongs in it.
Reject used c, r − c, or r + c.
Remove all three markers after exploring.
Exponential in n, roughly O(n!), with O(n) state and recursion depth.
Removing only the column but not both diagonal markers corrupts sibling branches.
Read the full guide first: Recursion and Backtracking.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.