N-Queens
Description: How can N queens be placed on an NxN chessboard so that no two of them attack each other?
Tags: amplpy, constraint-programming, highlights
Notebook author: Gleb Belov <gleb@ampl.com>
Modeling N-Queens with alldiff
N-Queens: How can N queens be placed on an NxN chessboard so that no two of them attack each other?
Constraint alldiff
enforces a set of integer variables to take distinct values. Using alldiff
, we can model N-Queens as follows:
Solving with HiGHS and displaying the solution
HiGHS 1.4.0: tech:outlev=0
0; Iter: Time 3.219e-07; average = 3.219e-08; Bound = 3.312e-05
HiGHS 1.4.0: optimal solution
0 simplex iterations
1 branching nodes
Objective = find a feasible point.
Solution
+ + + + + + + + + + + + + + + + + Q + +
+ + + + + + + + + + + + + Q + + + + + +
+ + + + + + + + + + + + + + + + + + Q +
+ + + + + + + + Q + + + + + + + + + + +
+ + + + + + + + + + + + Q + + + + + + +
+ + + + + + + Q + + + + + + + + + + + +
+ + + Q + + + + + + + + + + + + + + + +
Q + + + + + + + + + + + + + + + + + + +
+ + Q + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + Q + + +
+ + + + + + + + + Q + + + + + + + + + +
+ + + + + + + + + + + Q + + + + + + + +
+ Q + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + Q + + + + +
+ + + + Q + + + + + + + + + + + + + + +
+ + + + + + Q + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + Q
+ + + + + + + + + + + + + + + Q + + + +
+ + + + + + + + + + Q + + + + + + + + +
+ + + + + Q + + + + + + + + + + + + + +