On this page
N-Queens
Place N queens on N×N board.
Overview
Place N queens on N×N board.
Example
void backtrack(List<Integer> path, int start) {
// Add current path to results
for (int i = start; i < n; i++) {
path.add(i);
backtrack(path, i + 1);
path.remove(path.size() - 1);
}
}
Common Use Cases
- Combinatorial generation
- Constraint satisfaction
- Game solving
Pitfalls to Avoid
- Missing base case
- Not pruning invalid branches early
Related Topics
- Dynamic programming
- DFS on graphs
Best Practices
- Understand when to use n-queens versus simpler alternatives
- Write unit tests covering edge cases and failure paths
- Follow Java conventions and prefer standard library APIs when available
- Profile before optimizing — measure impact in your specific workload