Stage 1 · Code
Backtracking
N Queens
Place N queens on N×N board so no two attack each other.
Problem
Place N queens on N×N chessboard so no two queens attack each other (same row, column, or diagonal). Return all distinct solutions.
Backtracking with Sets
Place queens row by row. For each row, try all columns. Track used columns, positive diagonals (row+col), negative diagonals (row-col).
Bit Optimization
Use integers as bitsets for columns and diagonals. Much faster for larger N. col, diag1, diag2 are bitmasks. Available positions = ~(col | diag1 | diag2) & ((1<<n)-1).
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.