Looking at the N-queens problem

Imagine your friend challenges you to array eight queens on a standard chessboard so that none of the queens conflict with one another. This problem, known as N-queens, is a fundamental constraint satisfaction problem, similar to the knapsack problem introduced in the lesson Optimizing Cargo Loads. In N-queens, the objective is to configure N queens on a chessboard so that no queen threatens another. In chess, a piece is “threatened” when another piece can move to the square it occupies to “capture” it. The queen is permitted to move horizontally, vertically, and diagonally any number of spaces on the board. Because queens can move in any direction horizontally or vertically, it’s only possible to create a correct configuration of N queens on an NxN chessboard.

The following image illustrates a correct solution to the N-queens problem with eight queens on an 8x8 chessboard:

Get hands-on with 1200+ tech skills courses.