How to model the n-queens problem using linear programming

Chess is one of the oldest and most celebrated board games in history, with origins dating back over a thousand years. Traditionally played on an $8 \times 8$ board, chess involves strategy, tactics, and deep thinking, as each piece moves according to specific rules. Beyond the game itself, the chessboard and its pieces have inspired a variety of logic puzzles and mathematical challenges.

One of the most famous of these is the eight queens problem, which asks how to place eight queens on a standard chessboard so that no two queens threaten each other—meaning that no two queens share the same row, column, or diagonal. This puzzle has fascinated mathematicians and enthusiasts alike, as it combines combinatorial reasoning with spatial visualization.

The problem can also be generalized to place the maximum number of queens on a board of different dimensions, $m \times n$, creating a family of logic challenges with increasing complexity. For example, the board shown below represents one possible solution to the eight queens problem. On a standard chessboard, there are a total of 92 distinct solutions, making this a rich and engaging puzzle for anyone interested in logic, mathematics, and chess.

This problem can be formulated as a binary optimization model. Do you dare to try it?




If you found this useful, please cite this as:

Martín-Campo, F. Javier (Dec 2025). How to model the n-queens problem using linear programming. https://www.fjmartincampo.com/blog/2025/queens/.

or as a BibTeX entry:

@misc{martín-campo2025how-to-model-the-n-queens-problem-using-linear-programming,
  title   = {How to model the n-queens problem using linear programming},
  author  = {Martín-Campo, F. Javier},
  year    = {2025},
  month   = {Dec},
  url     = {https://www.fjmartincampo.com/blog/2025/queens/}
}



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Discovering the mirror of linear optimization, the fascinating world of duality
  • Convergence, Degeneracy, and the "Dark Side" of the Simplex
  • How to recognize the different types of solutions in linear optimization using the Simplex algorithm
  • Starting the Simplex Method, how to begin when no obvious basic solution exists
  • The Simplex Algorithm, the engine of mathematical optimization