The Eight Queens Puzzle is an old brainteaser that asks: How can you place eight queens on a chessboard so that no two queens are attacking each other?
It turns out there are many solutions, but here’s a particularly symmetrical one:

In Martin Gardner’s book “The Last Recreations”, I learned about a nice generalization of this problem that asks instead: How many queens can you place on a chessboard so that each queen is attacking exactly one other queen? Or attacking exactly two others? Or exactly three others? Etc. [These are “generalizations” in the sense that the original eight queens problem requires that each queen is attacking exactly zero other queens.]
For example, here are some optimal solutions to these questions:

This post describes how to find solutions like these, why they are optimal, and explores some other generalizations.
History & Known Results
While the Eight Queens problem has been around since the nineteenth century, and has been extensively researched, these other variants are more recent and have received less attention. Nonetheless, various results are known, which are summarized below.
First, let’s define
\[ \begin{aligned} & \text{Maximum number of queens that can be}\\ Q_k(n) \hspace{0.1in} = \hspace{0.2in} & \text{placed on an } n \times n \text{ chessboard so that each } \\ & \text{queen is attacking exactly } k \text{ other queens}. \end{aligned}\]
So, the examples in Figure 1 show that
\[ \begin{aligned} Q_1(8) &\geq 10 \\ Q_2(8) &\geq 14 \\ Q_3(8) &\geq 18 \\ Q_4(8) &\geq 21 \end{aligned}\]
Martin Gardner attributes the first questions about to Scott Kim in 1980-1981 (in the Journal of Recreational Mathematics). A survey by Bell & Stevens says that Gik (in the Soviet Union) also raised similar questions around the same time, and it appears that substantial progress was made by Peter Hayes in 1992 (also in the Journal of Recreational Mathematics). I haven’t been able to track down these original articles, so what follows is pieced together based on results reported by Gardner and Bell & Stevens, together with my own investigations.
Firstly, really only makes sense for
because it’s impossible to place queens (on a board of any size) so that every queen is attacking 5 or more other queens. (To see this, consider the right-most column that has a queen in it; if there are multiple queens in that column, choose the top-most one. That queen has no queens anywhere to its right, nor any directly above it, so of the 8 directions that queen can attack, at least 4 of them have no queens (SE, E, NE are all to the right and N is directly above); therefore, it can be attacking at most 4 queens.)
Evidently, the Hayes article completely settles the question of and
, giving an explicit formula for each of them, namely
(for
) and
(for
). This fact is documented in the OEIS here and here.
So the interesting cases are and
, and for both of these, some interesting upper-bounds are known.
Bounds when k = 1
According to Bell & Stevens, Hayes showed that ; a slightly sharper bound of
is given, without proof, by Jud McCrainie here. However, the two bounds are actually equivalent once you realize that
is always even; indeed, if every queen is attacking exactly one other queen, then the queens must all occur in pairs that are threatening each other.
And taking this argument one step further actually leads to a direct proof of the sharper bound:
Observation: .
Proof: Consider a pair of queens that are attacking each other. They must be in two different columns or two different rows (or both, if they are attacking each other diagonally). In either case, they occupy at least 3 rows/columns and there can be no other queens in those rows/columns. Since there are a total of 2n rows and columns, and each pair of queens stakes a claim to at least 3 of them, there can be at most pairs of queens, i.e., at most
queens total. QED
Incidentally, when , this gives a bound of
, showing that the configuration in Figure 1 is indeed optimal. Here are some other optimal configurations for small values of
:

More terms of the series (along with more examples) are given in A051754 of the OEIS, and later we’ll see how to actually find these solutions (and much larger ones!) using powerful “SAT Solver” algorithms.
Note that apart from and
all these examples achieve the bound
, leading to the following sensible conjecture (which has been verified for
).
Conjecture: for all
.
Bounds when k = 3
According to Bell & Stevens, Hayes also showed that . I’m not sure what the original proof was, but here is a way to see this:
Observation: .
Proof: Consider how the 8 “attack lines” from a queen can terminate at the edge of the board, as highlighted in red here:

If we consider all possible placements of queens on the board, then the set of all possible terminations looks like this:

From this figure, it’s easy to see that there are, in general, red segments in the corners of the board and
red segments on each side of the board, for a total of
red segments.
Now consider a configuration where each queen is attacking three others and pick a queen (i.e., the rightmost queen in the example below). Three of the queen’s eight attack lines are obstructed by the three queens she is attacking, but the five remaining attack lines must extend to the edge of the board, staking a claim to five of the red segments.

This is true for any queen in the configuration, and the red segments cannot be “shared” by more than one queen, so there must be at most queens in total, i.e.,
.
Finally, note that must be even. To see this, we can count the number of “lines of attack” between queens on the board: with
queens, each attacking
other queens, we have
lines of attack, but this actually counts each line of attack twice – once in each direction – so
must be even, which means
must be even.
So we have (which is always the largest even number
). QED
Finding Solutions
Most of the results above focus on upper-bounds on , but how can we actually find examples like those in Figure 1 and Figure 2? One effective way is to use a “SAT solver” (as we also did here). “SAT” is a shorthand for the “Boolean Satisfiability Problem“, which is essentially the problem of finding solutions to formulas where the variables are binary (i.e., the only valid values are TRUE=1 and FALSE=0). SAT is a foundational topic in computer science and has been studied extensively. While there are a variety of reasons to believe that SAT is a hard problem to solve, there are also impressive “SAT solvers” that do quite well on practical instances, which is what interests us here. To read (much) more about this fascinating topic, check out Chapter 7 of Donald Knuth’s The Art of Computer Programming.
To transform our problem into a SAT problem, we start by defining Boolean variables
, which will encode whether or not there is a queen in cell
:

And if we’re looking for a solution with at least queens, then we’ll add the constraint:
\[ \sum_i \sum_j x_{i, j} \geq m \]
to our SAT formula. (There are a variety of techniques for converting such “cardinality constraints” into SAT formulas. See Knuth, for example.)
Next we need to add constraints that say: “Each queen is attacking exactly other queens”. We’ll build these up incrementally as follows.
Suppose there is a queen in cell , and consider one of the
possible direction of attack from that cell (shown in red here):

We can define a Boolean variable that is true if and only if there is a queen in that direction of attack from cell $(4, 3)$, for example, using the clauses:
\[ (\neg x_{3,4} \vee z) \wedge (\neg x_{2, 5} \vee z) \wedge (\neg x_{1, 6} \vee z) \wedge (\neg z \vee x_{3, 4} \vee x_{2, 5} \vee x_{1, 6}). \]
In words: the first three clauses ensure that if there is a queen in , then
is true; the last clause ensures that if
is true then there is a queen in one of those three cells.
Now, if we’ve created variables like this for each of the
directions of attack, we can add a constraint that says:
\[ \text{If } x_{4, 3} \text{ then } \sum_d z_d = k.\]
[In practice, this can be done by creating the CNF for and then inserting
into each clause.]
Proceeding in this way for each of the cells, we obtain a collection of SAT clauses that encode the problem of finding valid configurations of queens, and which we can feed to a SAT solving algorithm.
So, how well does this work in practice? Quite well, it turns out . . .
Some Results
In the case of , this approach handily found examples for all
that match the upper bound of
. In some cases, this search could be sped-up by searching for symmetrical solutions. For example, in all cases except
and
, I was able to find solutions that had
symmetry, like:

And in some cases, there were optimal solutions with symmetry (i.e., all
rotations are the same), like this placement of
queens on an
board:

For , this approach was also able to extend OEIS sequence A051756 up to
, all of which match the upper bound of
. This included some particularly symmetrical solutions, like the following configuration of
queens on a
board:

However, once we get to , the SAT solver starts to really struggle to find solutions . It’s hard to say whether this is because there is no solution that meets the upper bound, or whether the SAT solver simply gets lost in the exponentially large search space. Despite this apparent difficulty, it effortlessly finds solutions that are just below the bound, such as this configuration of 40 queens on an
board:

Is this optimal, or is there a configuration with queens?
Update [11-Feb-2024]: There is indeed a configuration of queens on an
board, showing that
(and not 40)!

The key to getting the SAT solver to find such a solution was to provide some “hint” clauses constructed using the following logic.
In the proof of the upper bound on above, we established that there are
“terminations” and that each queen must cover exactly 5 of them. That means that when
, there are
terminations and a configuration with
queens would cover
of them, leaving exactly two terminations unused. It’s not hard to see that these two unused terminations must be “pointing toward each other” and this means that the line joining these terminations has no queens in it. In particular, there must be some row/column/diagonal with no queens in it, and all other rows/columns/diagonals must contain at least one queen. This statement can easily be expressed as a SAT formula (e.g., by defining a new variable for each row/column/diagonal and defining that variable as “there is no queen along this line”, and then adding clauses saying that exactly one of these variables is true).
Remarkably, adding these “hints” to the formula passed to the SAT solver, allows it to find a solution is seconds (rather than spinning its wheels for days, as it did before being given these hints)!
And this trick doesn’t just help for . Essentially the same idea has helped find optimal configurations for
up to at least 117. In general, there may be 0, 2, 4, 6, or 8 unused terminations, in which case there are (respectively) exactly 0, 1, 2, 3, or 4 rows/columns/diagonals with no queens in them, but any of these conditions is easily expressed as SAT clauses.
So why are these hints so helpful? Well, SAT solving is mysterious enough that it’s hard to say exactly “why” some formulas are easier than others, but it’s often the case that SAT solvers struggle with the kind of Pigeonhole Principle reasoning that we used in developing the “hints”. (Indeed, Knuth shows how some SAT algorithms require exponential time to prove that pigeons can’t fit in
pigeonholes.) By using our brains to do some of the pigeonhole thinking, and then feeding the insights to the SAT solver, it can presumably search much more intelligently.
Now that we’ve been able to calculate for many more values, it’s reasonable to make the following conjecture:
Conjecture: for all
.
Other Generalizations
Above, we focused on the function in part because this was the generalization of the n-Queens Problem that previous authors looked at. Arguably, however, it would be just as natural to study the function
defined by:
\[ \begin{aligned} & \text{Maximum number of queens that can be}\\ q_k(n) \hspace{0.1in} = \hspace{0.2in} & \text{placed on an } n \times n \text{ chessboard so that each } \\ & \text{queen is attacking at most } k \text{ other queens}. \end{aligned}\]
This is also a natural generalization in the sense that is the classic n-Queens Problem.
Since is less restrictive than
(by not requiring that every queen be attacking
others), it follows that
.
And when it comes to upper bounds, we’ve already done most of the work. Indeed, the proof above that , readily generalizes to a proof of the following:
Observation:
This is a bit looser than the known bounds on , but that’s at least in part because
in some cases. For example, this configuration of
queens on a
board shows that
, which is optimal:

in contrast to .
Another nice feature of is that it actually makes sense for values of
greater than
(unlike
). For example,
:

I.e., each queen is attacking at most other queens or, put another way, each queen has at least one unobstructed “line of sight” to the edge of the board. (We know that
is the optimal value because a SAT solver was quickly able to show there were no solutions with at least
queens.)
And it turns out that can be pinned down exactly…
Observation: for all
.
Proof: First, to see that , note that we can simply arrange
queens around the edge of the board:

and each queen is attacking its two “neighbors” and at most one other queen on each of the other three sides of the board, i.e., at most other queens.
To see that , take some configuration of
queens where each queen is attacking at most
others, and consider the convex hull of these queens. If the vertices of the convex hull are
, then there must be a queen at each such vertex,
, and the number of other queens she is attacking,
, must satisfy:
\[ 45^\circ (a_i – 1) \leq \theta_i ,\]
where is the interior angle of the convex hull at
.
Now, a polygon with vertices has interior angles that sum to
, which means:
\[ \begin{aligned} 45^\circ \sum_i (a_i – 1) & \leq 180^\circ \cdot (h – 2) \\ \left(\sum_i a_i \right) – h & \leq 4(h – 2) \\ \sum_i a_i & \leq 5h – 8 \end{aligned} \]
Now, we count “terminations”, as we did in previous proofs. If a queen is attacking other queens, then she must cover
of the
terminations on the boundary of the board. In particular, the
queens at the vertices of the convex hull consume at least
\[ \sum_i (8 – a_i) = 8h – \sum_i a_i \geq 8h – (5h – 8) = 3h + 8 \]
terminations. And the remaining queens each consume at least
terminations (since they are each attacking at most
queens). Therefore, we have
\[ 3h + 8 + 3(m-h) \leq 12n – 4 \]
and solving for yields
. QED
And similar reasoning can also be used for :
Observation: for all
.
Proof: The following arrangement shows that :

To see that , we will count “terminations”. Suppose we have a configuration where each queen is attacking at most
others. There are
terminations, and each queen must cover at least
of them, implying that there can be at most
queens. However, to achieve this bound of $3n – 1$ queens, each queen must cover exactly
terminations and every termination must be covered by some queen, and we’ll see this is not possible. Indeed, consider a corner of the board and the two terminations circled below:

For these terminations to be covered, there must be a queen in this corner square; however, if there’s a queen in this corner square, that queen covers terminations (not
). So it’s not possible for there to be
queens, and therefore the maximum possible is
. QED
Conclusions
In this post, we’ve made some progress in understanding and
, and along the way we’ve extended OEIS sequences A051754 and A051756. But in some ways we’ve only scratched the surface of this topic and there are many open questions. The survey by Bell & Stevens describes many other directions of inquiry into the n-Queens Problem and related questions. And perhaps some readers of this blog will be able to prove the conjectures described here, or find even larger examples of optimal configurations!
Leave a Reply