

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: N Queens
incomplete
2: Validate Boards
incomplete
3: First Solution
incomplete
4: All Solutions
incomplete
5: Safe Positions
incomplete
6: Backtracking
incomplete
7: Pruning
incomplete
8: Complexity
incomplete
This lesson's interactive features are locked, please to keep using them
We already have the logic needed to solve N Queens in a blunt way:
This is a brute force solution. We're not being clever, we're just searching through the entire space of possibilities until we find an answer.
For n = 4, the first valid board we hit while checking candidates in lexicographic order is:
[1, 3, 0, 2]
Which represents the board you're probably tired of seeing by now:
Not every board size has a solution. There's no way to solve N Queens on a 2 × 2 or 3 × 3 board, so the correct answer in those cases is an empty list ([]).
Complete the solve_n_queens function. It accepts a board size n and returns the first valid board it finds, or an empty list if no valid board exists.
The get_candidate_boards and is_valid_board functions are provided for you.
We won't test anything beyond n = 7 here, since the brute-force approach of generating all possible candidate boards requires exponentially increasing amounts of memory.
Running this with n = 8 would try to allocate more than 2 GB!