ArticlePDF Available

# An alternative algorithm for the n –Queens puzzle

Authors:

## Abstract and Figures

In this paper a new method for solving the problem of placing n queens on a n×n chessboard such that no two queens directly threaten one another and considering that several immovable queens are already occupying established positions on the board is presented. At first, it is applied to the 8–Queens puzzle on a classical chessboard and finally to the n Queens completion puzzle. Furthermore, this method allows finding repetitive patterns of solutions for any n .
Content may be subject to copyright.
An alternative algorithm for the n–Queens
puzzle
David Luque Sacaluga
Chiclana de la Frontera 11130 C´adiz, Spain
Abstract
In this paper a new method for solving the problem of placing nqueens on a n×n
chessboard such that no two queens directly threaten one another and considering
that several immovable queens are already occupying established positions on the
board is presented. At ﬁrst, it is applied to the 8–Queens puzzle on a classical
chessboard and ﬁnally to the nQueens completion puzzle. Furthermore, this method
allows ﬁnding repetitive patterns of solutions for any n.
Keywords: Chessboard; n–Queens puzzle; NP-completeness; Patterns
puzzle in a colloquial way, what kind of algorithms are used for solving it and what is
the diﬀerence between the 8–Queens puzzle and the 8–Queens completion problem. Also,
it is clariﬁed that the paper is not related to the P vs NP Millennium Prize Problem.
A second section introduces the concept of subpanels when a chessboard is broken
down. A limited number of queens allowed in every subpanel are deﬁned. An equation
representing the several cases is deﬁned and a graphical example solution is shown.
The third section is an analysis of the method applied for any value of n. It takes
into account if nis even or odd. The diﬀerent cases depend on the number of queens
in the ﬁrst and last subpanels and the subcases depend on the number of queens to be
distributed in the central subpanels. The several alternatives will depend on the position
of the queens into subpanels. A graphical representation for helping to understand every
case is included. In this point we observe that when nis increased then the number of
cases, subcases and alternatives increase too, but when the number of queens preplaced is
increased then the number of alternatives, subcases and cases decrease. We can conclude
that the problem becomes easiest when the number of queens preplaced is increased. At
the end of this section, some pattern rules to extend solutions from smallest to largest
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
40 An alternative algorithm for the n–Queens puzzle
values of nare explained with two simple examples. We invite to the reader for ﬁnding
patterns rules from other solutions of the 8–Queens puzzle.
Finally, the appendix is a “time travel to 1850.” We don’t have a computer and
Someone is asking to ﬁnd all solutions with two queens preplaced at position b4 and d5.
Fortunately, we have travelled with a complete analysis of the completion problem in
our knapsack.
1 Introduction
In September 2017, a lot of news regarding how to win one million dollars appeared
on the internet. The reason was a paper published about the complexity of the n–Queens
Completion Problem by researchers at the University of St. Andrews [GJN17]. Obviously,
curiosity led many uninitiated people to these complex problems (like me) trying to solve
it. It was about the millennium problem: P = NP? [CMA21], [Coo71], but focusing on
a chessboard.
In September 1848, a long time before, Max Bezzel published the 8–Queens puzzle
and two years later, Frank Nauck found the ﬁrst solutions. The method used by Nauck
begun by placing a queen on the ﬁrst column and placing the others queens on the lowest
unguarded squares [Sch15], similar to brute force and backtracking techniques used today
by programmers but in a time when there were no computers. Brute force is useful for
small domains, due to large overheads in sophisticated approaches. Backtracking is an
improvement of the brute force approach, which systematically searches for a solution to
a problem among all available options; i.e. backtracking checks each possibility until they
ﬁnd the right one. It is a depth–ﬁrst search of the whole of possible solutions. During the
searching, if an alternative is not allowed then it backtracks to the place which presented
several alternatives, and tries the next one. If the alternatives are exhausted, then it goes
back to the previous choice point and tries the next alternative. If there are no more
options, then the search fails [BSR08], [BSR10], [ACP68].
Back to 1850, Nauck introduced the n–Queens Completion puzzle presenting a variant
of the 8–Queens puzzle in which some queens were already placed on the chessboard and
he asked how to place the rest of the queens [ACJ18].
On one hand, the n–Queens puzzle is the problem to place nqueens on an nby n
chessboard in such a way that none of the queens is attacking another queen and on the
other hand, the n–Queens Completion puzzle is the problem to place k < n queens on a
nby nchessboard in which (nk) queens are already placed.
Let’s try to solve the 8–Queens puzzle. Can you place eight queens on a chessboard
in such a way that none of the queens is attacking another queen? It’s not so hard, is
it? You can try it and ﬁnally you will get a solution. To check that a given solution is
correct is easy because once the queens are placed, only one queen is allowed in every
row, column or diagonal. But, can you ﬁnd all the solutions? Why not? If the chessboard
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 41
is ﬁnite then it is only a patient and time question. What about when nis increased?
You will have more solutions and you will need more time to ﬁnd them.. . but if you
have got a computer and a good algorithm then the problem is not so hard. Currently,
the algorithms used for solving this kind of problems are very eﬃcient when the number
of queens is moderated but not too eﬃcient with “monster” values of n. Up to date, the
27 ×27 chessboard is the higher value calculated with 234,907,967,154,122,528 solutions
[?].
There are several strategies for solving the n–Queens puzzle [AY89], [BS09], [BD75],
[BM02], [Eng07], [ET92], [Ber91]. For instance, there is a simple way to extend a solution
for dimension (n1)×(n1) to dimension n×n. Figure 1 shows a solution that exhibits
stair–stepped patterns for n= 8 to n= 9 by adding a queen in a corner square of the
chessboard.
Figure 1: Example of extended solution for dimension n= 8 to n= 9
The problem becomes more diﬃcult when several immovable queens are already
occupying established positions on the board, that is, “checking” becomes easier because
there are fewer options, but “solving” becomes more diﬃcult because the immovable
queens preplaced don’t ensure the existence of a solution. For instance, Figure 2 shows
two queens preplaced on the chessboard. Can you place six queens? Now, no solution is
allowed.
Figure 2: Example of 8–Queens completion puzzle.
The “alternative” algorithm presented in this paper could be a “sophisticated” back-
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
42 An alternative algorithm for the n–Queens puzzle
tracking algorithm to be used for speeding up the process because processing several
cases in any order and try the most constrained ones ﬁrst to obtain a solution from a
current partial solution is allowed. Anyway, it is just an algorithm without the claim
that it works in polynomial time and it does not concern the P = NP problem. Never-
theless, if you ﬁnd an algorithm to determine the existence of a solution of the n–Queens
completion puzzle, it could be applied for solving other NP–complete problems [GJ79],
[?].
2 Method description on the 8–Queens puzzle
The method is based on breaking down a chessboard in several subpanels. Considering
the game’s rule that not two queens can share the same row, column, or diagonal, a
minimum and maximum number of queens allowed on every subpanel is deﬁned. From
these data, an equation and a list of possible cases and subcases are deﬁned. Depending
on the number and the position of queens on every subpanel, the reconstruction of the
chessboard can be performed. Solution appears when constraints are satisﬁed.
A 64–square chessboard can be broken down into four subpanels as shown in Figure 3.
Figure 3: Breaking down of a chessboard in four subpanels.
Let be S4the subpanel shaped by d4, d5, e4 and e5 squares. A minimum of zero
queens and a maximum of one queen for placing on subpanel S4will be allowed.
Figure 4: Example of minimum and maximum number of queens allowed on subpanel
S4.
Let be S12 the subpanel shaped by c3, c4, c5, c6, d3, d6, e3, e6, f3, f4, f5 and f6 squares.
A minimum of zero queens and a maximum of four queens for placing on subpanels S12
will be allowed.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 43
Figure 5: Example of minimum and maximum number of queens allowed on subpanels
S12.
Let be S20 the subpanel shaped by b2, b3, b4, b5, b6, b7, c2, c7, d2, d7, e2, e7, f2,
f7, g2, g3, g4, g5, g6 and g7 squares. A minimum of zero queens and a maximum of four
queens for placing on subpanels S20 will be allowed.
Figure 6: Example of minimum and maximum number of queens allowed on subpanels
S20.
Let be S28 the subpanel shaped by a1, a2, a3, a4, a5, a6, a7, a8, b1, b8, c1, c8, d1,
d8, e1, e8, f1, f8, g1, g8, h1, h2, h3, h4, h5, h6, h7 and h8 squares. A minimum of three
queens and a maximum of four queens for placing on subpanel S28 will be allowed.
Figure 7: Example of minimum and maximum number of queens allowed on subpanel
S28
Let be T64 a chessboard shaped by 64 squares. Namely T64 is shaped by S4,S12,S20
and S28 subpanels. Considering the eight queens to be placed and the number of queens
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
44 An alternative algorithm for the n–Queens puzzle
allowed on every subpanel, the several cases allowed for the 8–Queens puzzle are deﬁned
in Equation (1).
T64 {8}=S4(1
0+S12
4
3
2
1
0
+S20
4
3
2
1
0
+S28 (4
3(1)
In any case, the total number of queens on the chessboard must be equal to eight
and the reconstruction of the chessboard depends on the position of the queens at every
subpanel. An example is showed in Figure 8.
Figure 8: Example of breaking down graphical representation of an 8–Queens puzzle
solution.
3 Method description on a n×nchessboard
The method can be used for solving the question about how to place nqueens on
an×nchessboard considering that several immovable queens are already occupying
established positions on the board, but it depends on nis even or odd. Also, some
pattern rules can be applied from lower values to higher values of n.
3.1 When nis even
Let nbe an even integer number. The number of squares on a n×nchessboard is
equal to the sum of the number of squares on every subpanel:
n2= 4 + 12 + 20 + 28 + · · · +n2(n2)2
Let Tnxn be a chessboard with n×nsquares and let S4,S12,S20,S28, . . . , Sn2(n2)2
be the subpanels. If we assume n > 2, the several cases considering the number of queens
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 45
allowed on every subpanel is deﬁned in Equation (2).
Tnxn {n}=S4(1
0+S12
4
3
2
1
0
+S20
4
3
2
1
0
+S28
4
3
2
1
0
+S36
4
3
2
1
0
+· · · +Sn2(n2)2(4
3(2)
Equation (2) can be expressed as the Equation (3).
Tnxn {n}=S4(1
0+
n4
2
X
i=1
S12+8(i1)
4
3
2
1
0
+Sn2(n2)2(4
3(3)
In any case, the total number of queens in the subpanels is equal to n. Since the
subpanels S4and Sn2(n2)2have less choices, we will split the analysis into four cases
depending on the number of queens in these subpanels. They are deﬁned in the Table 1.
Cases Tn×nS4
n4
2
P
i=1
S12+8(i1) Sn2(n2)2
Case 1 n1n54
Case 2 n0n44
Case 3 n1n43
Case 4 n0n33
Table 1: List of four possible cases when nis even.
Depending on the number of queens allowed on the subpanels S12+8(i1), there are
several possible subcases. It is represented in Equation 4.
n4
2
X
i=1
S12+8(i1) =
Case 1 : n5
Case 2 : n4
Case 3 : n4
Case 4 : n3
(4)
Now we proceed to analyze each case one by one.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
46 An alternative algorithm for the n–Queens puzzle
Case 1: The following constraints must be satisﬁed:
There is one queen on subpanel S4.
There are four queens on subpanel Sn2(n2)2. So, it is not allowed to place any
queen in the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S12+8(i1) is n5.
Figure 9: Case 1 when nis even. Example of graphical representation.
Case 2: The following constraints must be satisﬁed:
There is not any queen on subpanel S4.
There are four queens on subpanel Sn2(n2)2. So, it is not allowed to place any
queen in the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S12+8(i1) is n4.
Figure 10: Case 2 when nis even. Example of graphical representation.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 47
Case 3: The following constraints must be satisﬁed:
There is one queen on subpanel S4.
There are three queens on subpanel Sn2(n2)2. So, at least one queen must be
placed into one of the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S12+8(i1) is n4.
Figure 11: Case 3 when nis even. Example of graphical representation
Case 4: The following constraints must be satisﬁed:
There is not any queen on subpanel S4.
There are three queens on subpanel Sn2(n2)2. So, at least one queen must be
placed into one of the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S12+8(i1) is n3.
Figure 12: Case 4 when nis even. Example of graphical representation.
Constraints will be conﬁgured according to the squares where some queens are already
placed but in any case the following restrictions can be conﬁgured:
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
48 An alternative algorithm for the n–Queens puzzle
There are two alternatives on subpanel S4; there is one queen or there is not any
queen.
There are two alternatives on subpanel Sn2(n2)2; there are four queens or there
are three queens:
If there are four queens then it is not allowed to select the corners square.
If there are three queens then one corner square must be selected.
It is not allowed to select more than two subpanels where a corner is selected.
The total number of queens on the chessboard is equal to n.
3.2 When mis odd
Let mbe an odd integer number. The number of squares on a m×mchessboard is
equal to the sum of the number of squares on every subpanel:
m2=1+8+16+24+· · · +m2(m2)2
Let Tmxm be a chessboard with m×msquare and let S1,S8,S16,S24, . . . Sm2(m2)2
be the subpanels. If we assume m > 3, the several cases considering the number of queens
allowed on every subpanel are deﬁned in Equation (5).
Tmxm {m}=S1(1
0+S8
2
1
0
+S16
4
3
2
1
0
+S24
4
3
2
1
0
+· · · +Sm2(m2)2(4
3(5)
In this case S1represents the ﬁrst subpanel or the center of the chessboard and S8
represents the second subpanel. Equation (5) can be expressed as the Equation (6).
Tmxm {m}=S1(1
0+S8
2
1
0
+
m5
2
X
i=1
S16+8(i1)
4
3
2
1
0
+Sm2(m2)2(4
3(6)
In any case, the total number of queens in the subpanels is equal to m. Since the
subpanels S1,S8and Sm2(m2)2,have less choices, we will split the analysis into seven
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 49
Cases Tmxm S1S8
m5
2
P
i=1
S16+8(i1) Sm2(m2)2
Case 1 m1 0 m54
Case 2 m0 2 m64
Case 3 m0 1 m54
Case 4 m0 0 m44
Case 5 m0 2 m53
Case 6 m0 1 m43
Case 7 m0 0 m33
Table 2: List of seven possible cases when m is odd.
cases depending on the number of queens in these subpanels. They are deﬁned in the
Table 2.
Depending on the number of queens allowed on the subpanels S16+8(i1), there are
several possible subcases. It is represented in Equation (7).
m5
2
X
i=1
S16+8(i1) =
Case 1 : m5
Case 2 : m6
Case 3 : m5
Case 4 : m4
Case 5 : m5
Case 6 : m4
Case 7 : m3
(7)
Now we proceed to analyze each case one by one.
Case 1: The following constraints must be satisﬁed:
There is one queen on subpanel S1. So, it is not allowed to place any queen in the
principal diagonals and central squares of the board.
There is not any queen on subpanel S8.
There are four queens on subpanel Sm2(m2)2.
The number of queens to be distributed on the subpanels S16+ 8(i1) is m5.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
50 An alternative algorithm for the n–Queens puzzle
Figure 13: Case 1 when mis odd. Example of graphical representation.
Case 2: The following constraints must be satisﬁed:
There is not any queen on subpanel S1.
There are two queens on subpanel S8. So, at least one queen must be placed into
one of the corner squares of this subpanel.
There are four queens on subpanel Sm2(m2)2. So, it is not allowed to place any
queen in the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S16+8(i1) is m6.
Figure 14: Case 2 when mis odd. Example of graphical representation.
Case 3: The following constraints must be satisﬁed:
There is not any queen on subpanel S1.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 51
There is one queen on subpanel S8.
There are four queens on subpanel Sm2(m2)2. So, it is not allowed to place any
queen in the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S16+8(i1) is m5.
Figure 15: Case 3 when mis odd. Example of graphical representation.
Case 4: The following constraints must be satisﬁed:
There is not any queen on subpanel S1.
There is not any queen on subpanel S8.
There are four queens on subpanel Sm2(m2)2. So, it is not allowed to place any
queen in the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S16+8(i1) is m4.
Figure 16: Case 4 when mis odd. Example of graphical representation
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
52 An alternative algorithm for the n–Queens puzzle
Case 5: The following constraints must be satisﬁed:
There is not any queen on subpanel S1.
There are two queens on subpanel S8.
There are three queens on subpanel Sm2(m2)2. So, at least one queen must be
placed into one of the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S16+8(i1) is m5.
Figure 17: Case 5 when mis odd. Example of graphical representation.
Case 6: The following constraints must be satisﬁed:
There is not any queen on subpanel S1.
There is one queen on subpanel S8.
There are three queens on subpanel Sm2(m2)2. So, at least one queen must be
placed into one of the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S16+8(i1) is m4.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 53
Figure 18: Case 6 when mis odd. Example of graphical representation.
Case 7: The following constraints must be satisﬁed:
There is not any queen on subpanel S1.
There is not any queen on subpanel S8.
There are three queens on subpanel Sm2(m2)2. So, at least one queen must be
placed into one of the corner squares of this subpanel.
The number of queens to be distributed on the subpanels S16+8(i1) is m3.
Figure 19: Case 7 when mis odd. Example of graphical representation.
Constraints will be conﬁgured according to the squares where some queens are already
placed but in any case the following restrictions can be conﬁgured:
There are two alternatives on subpanel S1; there is one queen or there is not any
queen. If there is one queen then it is not allowed to place any queen in the principal
diagonals of the board.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
54 An alternative algorithm for the n–Queens puzzle
There are three alternatives on subpanel S8; there are two queens, there is one
queen or there is not any queen. If there are two queens on this subpanel then one
corner square must be selected.
There are two alternatives on subpanel Sm2(m2)2; there are four queens or there
are three queens:
If there are four queens then it is not allowed to select the corners square.
If there are three queens then one corner square must be selected.
It is not allowed to select more than two subpanels where a corner is selected.
The total number of queens on the chessboard is equal to m
Finally, the cases and sub-cases list will depend on:
The number of queens pre-placed; in general, when the number of queens pre-placed
increase, the list of sub-cases decrease.
The position of the queens preplaced; for example, if nis even and there is one
queen on subpanel S4and three or four queens on subpanel Sn2(n2)2then the
list of sub-cases would decrease.
A complete analysis of a completion problem in a 8 ×8 chessboard is included in the
Appendix.
3.3 Pattern rules for extended solutions
This section shows how the method for breaking down an nby nchessboard could
be apply for ﬁnding repetitive patterns of solutions.
Let C > 0 be an even integer number. A solution of the n–Queens puzzle can be
extended to another solution of the n0–Queens puzzle with n0=n+C. The value of Cis
a proportional relation between the number of subpanels of nand n0. The pattern rule
we propose is based on the sequence of the number of queens placed from the lowest
to the highest subpanels of the smallest chessboard and also on a repetitiveness in the
position of the queens on the chessboard for dimension nto dimension n0.
Now two example solutions for dimension n= 8 will be extended for higher dimension.
Example 1. In Figure 20 we show a solution of the 8–Queens puzzle where several colors
have been used for helping to the searching of the pattern.
According to the number of queens placed on subpanels and the position of the queens
on the chessboard, some hypothesis can be assumed:
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 55
Figure 20: Example of 8–Queen puzzle solution.
There won’t be any queen on the ﬁrst subpanel S4and there will be four queens
on the last subpanel Sn2(n2)2
Two queens will be placed on the corners of the internal subpanels S12+8(i1)
(marked in red color).
The positions of the queens will keep some repetitiveness for dimension nto di-
mension n0
The value of n0can be found by testing C > 0. Figure 21 shows a solution for n0= 20
as an extended solution of n= 8.
The number of queens placed on every subpanel with C= 12 and for n= 8 to
extended solutions is showed in Table 3.
Subpanels
n4 12 20 28 36 44 52 60 68 76 84 92 100 108 116 124 . . .
8 0 2 2 4
200002242424
32 0 0 0 0 0 2 2 4 2 4 2 4 2 4 2 4
44 0 0 0 0 0 0 0 2 2 4 2 4 2 4 2 4 .. .
. . . 0 0 0 0 0 0 0 0 0 2 . . .
Table 3: Representation of the number of queens on subpanels for Example 1.
One approach is:
There is not any queen on subpanel S4.
There are four queens on subpanel Sn2(n2)2.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
56 An alternative algorithm for the n–Queens puzzle
Figure 21: Example of 20–Queens puzzle solution.
The number of subpanels with zero queens for dimension n= 8 to higher dimen-
sions will follow the sequence 1–3–5–7 . . .
A subpanel with two queens will be ﬁxed after the last subpanel with zero queens
and one queen must be placed on the top left corner square (red color).
The number of subpanels with two queens for dimension n= 8 to higher dimensions
will follow the sequence 2–4–6–8 . . .
The number of subpanels with four queens for dimension n= 8 to higher dimen-
sions will follow the sequence 1–3–5–7 . . .
The number of queens to be put on the subpanels is showed in the compact repre-
sentation.
Tn×n{n}=
n8
6
X
i=0
S4+8i{0}+S4
3(n+1) {2}+
n4
4
X
i=n+4
12
S4+16i{2}+
n4
4
X
i=n+4
12
S12+16i{4}
For instance, the equation to determine the number of queens to put on the subpanels
for n0= 2000 is showed in the following compact representation.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 57
T4000000 {2000}=
332
X
i=0
S4+8i{0}+S2668 {2}+
499
X
i=167
S4+16i{2}+
499
X
i=167
S12+16i{4}
Example 2. In Figure 22 we show another solution of the 8–Queens puzzle where several
colors have been used for helping to the searching of the pattern.
Figure 22: Example of 8–Queens puzzle solution.
According to the number of queens placed on subpanels and the position of the queens
on the chessboard, some hypothesis can be assumed:
There won’t be any queen on the ﬁrst subpanel S4and there will be three queens
on the last subpanel Sn2(n2)2.
One queen will be placed on the corner of the internal subpanels S12+8(i1) (marked
in orange color).
The positions of the queens will keep some repetitiveness for dimension nto di-
mension n0.
The value of n0can be found by testing C > 0. Figure 23 shows a solution for n0= 20
as an extended solution of n= 8.
The number of queens placed on every subpanel with C= 12 and for n= 8 to
extended solutions is showed in Table 4.
One approach is:
There is not any queen on subpanel S4.
There are three queens on subpanel Sn2(n2)2.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
58 An alternative algorithm for the n–Queens puzzle
Figure 23: Example of 20–Queen puzzle solution.
Subpanels
n4 12 20 28 36 44 52 60 68 76 84 92 100 108 116 124 . . .
8 0 2 3 3
200002324243
32 0 0 0 0 0 2 3 2 4 2 4 2 4 2 4 3
44 0 0 0 0 0 0 0 2 3 2 4 2 4 2 4 2 .. .
. . . 0 0 0 0 0 0 0 0 0 2 3 2 4 . . .
Table 4: Representation of the number of queens on subpanels for Example 2.
The number of subpanels with zero queens for dimension n= 8 to higher dimen-
sions will follow the sequence 1–3–5–7 . . .
A subpanel with two queens will be ﬁxed after the last subpanel with zero queens.
A subpanel with three queens will be ﬁxed after the ﬁrst subpanel with two queens
and one queen must be placed on the top left corner square (orange color).
The number of subpanels with two queens for dimension n= 8 to higher dimensions
will follow the sequence 1–3–5–7 . . .
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 59
The number of subpanels with four queens for dimension n= 8 to higher dimen-
sions will follow the sequence 0–2–4–6 . . .
The number of queens to be put on the subpanels is showed in the compact repre-
sentation.
Tn×n{n}=
n8
6
X
i=0
S4+8i{0}+S4
3(n+1) {2}+S4
3(n+7) {3}+
n8
4
X
i=n+4
12
S12+16i{2}+
+
n8
4
X
i=n+4
12
S20+16i{4}+S28+4(n8) {3}
In general, solutions of the n–Queens puzzle can be extended to higher dimensions
and it could be applied for solving n–Queens completion problems.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
60 An alternative algorithm for the n–Queens puzzle
Appendix: Complete analysis of a completion problem in a
8×8chessboard
In this appendix we will give a complete example of how the algorithm presented in
this paper would be applied to solve the 8–Queens completion puzzle, in the instance
proposed by Nauck in 1850 [ACJ18], see Figure 1. Two queens are already placed on the
chessboard and the solver is asked for placing the other six queens.
Figure 1: Example of two queens preplaced on the chessboard.
According to our algorithm we have the following constraint on the subpanels:
There is one queen placed on subpanel S4and it must be placed on d5 square.
There is at least one queen on subpanel S20 and it must be placed on b4 square.
The total number of queens in the subpanels S4,S12,S20 and S28 must be equal
to eight.
We can specify Equation (1) putting only the cases allowed by the position of the
queens which are already on the board:
T64 {8}= S4{1}+S12
4
3
2
1
0
+ S20
4
3
2
1
+ S28 (4
3(1)
The fact that the subpanel S4has only one possibility reduces the cases. It is rep-
resented in Table 1.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 61
T8x8S4S12 S20 S28
Case 1 8 1 - - 4
Case 2 8 1 - - 3
Table 1: List of two possible cases for the example.
Case 1: There is one queen on subpanel S4, there are four queens on subpanel S28
and there is at least one queen on subpanel S20. It is represented in Equation (2).
T64 {8}=S4{1}+S12
3
2
1
0
+S20
3
2
1
+S28 {4}(2)
In this case, the number of queens to be distributed on subpanels S12 and S20 must
be equal to 3. It is represented in Equation (3).
S12
2
1
0
+S20
3
2
1
= 3 (3)
Case 2: There is one queen on subpanel S4, there are three queens on subpanel S28
and there is at least one queen on subpanel S20. It is represented in Equation (4).
T64 {8}=S4{1}+S12
3
2
1
0
+S20
4
3
2
1
+S28 {3}(4)
In this case, the number of queens to be distributed on subpanels S12 and S20 must
be equal to 4. It is represented in Equation (5).
S12
3
2
1
0
+S20
4
3
2
1
= 4 (5)
Subcases allowed are listed in Table 2 and represented in Figure 2.
Now we proceed to analyze each subcase one by one. The solutions will be found
depending on subcases constraints.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
62 An alternative algorithm for the n–Queens puzzle
Sub-cases T64 S4S12 S20 S28
Sub-case 1.1 8 1 0 3 4
Sub-case 1.2 8 1 1 2 4
Sub-case 1.3 8 1 2 1 4
Sub-case 2.1 8 1 0 4 3
Sub-case 2.2 8 1 1 3 3
Sub-case 2.3 8 1 2 2 3
Sub-case 2.4 8 1 3 1 3
Table 2: List of sub-cases allowed for cases 1 and 2.
Figure 2: Graphical representation of subcases allowed.
Sub-case 1.1: There is no queen on subpanel S12 and there are three queens on
subpanel S20. (See Table 2)
The following constraints can be conﬁgured:
There is one queen on subpanel S4and it must be placed in d5 square.
There is not any queen on subpanel S12.
There are three queens on subpanel S20 and one must be placed in b4 square.
There are four queens on subpanel S28. So, it is not allowed to place any queen in
a1, a8, h1 and h8 squares.
Chessboard reconstruction from subpanel S4is represented in Figure 3. The squares
allowed for placing a queen on subpanel S20 are c2, e2, f2, g3, g6, g7 and c7.
To satisfy the constraints for placing three queens on subpanel S20, the alternatives
are {b4, c2, g3},{b4, c2, g7},{b4, e2, g3},{b4, e2, g6},{b4, e2, g7},{b4, e2, c7},{b4,
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 63
Figure 3: Sub-case 1.1. Squares allowed on sub-panel S20.
Figure 4: Sub-case 1.1. Graphical representation of the several alternatives for placing
three queens on subpanel S20.
f2, g6},{b4, f2, g7},{b4, f2, c7}and {b4, g6, c7}squares. Figures 4 and 5 represent the
several alternatives.
If {b4, c2, g3}squares are selected then the squares allowed on subpanel S28 are a7,
a6, f1, h6 and e8. The squares a1 and h8 are not allowed because of the constraint
to put four queens on subpanel S28, and not for the constraints of the queens
selected on subpanel S20. To satisfy the constraints for placing four queens on
subpanel S28, the alternatives are {a6, f1, h6, e8}and {a7, f1, h6, e8}. If {a6, f1,
h6, e8}is selected no solution is possible because a queen placed in a6 square would
be attacking to a queen placed in f1 or h6 squares. If {a7, f1, h6, e8}is selected
there is a solution because none of the queens is attacking another queen.
If {b4, c2, g7}squares are selected then the squares allowed on subpanel S28 are
a6, f1, h3 and e8. No solution is possible because a queen placed in f1 square would
be attacking to a queen placed in a6 or h3 square.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
64 An alternative algorithm for the n–Queens puzzle
If {b4, e2, g3}squares are selected then the squares allowed on subpanel S28 are
a7, c1, h6, h7 and c8. No solution is possible because a queen placed in c1 square
would be attacking to a queen placed in c8 square.
If {b4, e2, g6}squares are selected then the squares allowed on subpanel S28 are
a7, c1, h3 and c8. No solution is possible because a queen placed in c1 square would
be attacking to a queen placed in c8 square.
If {b4, e2, g7}squares are selected then the squares allowed on subpanel S28 are
c1, h3 and c8. No solution is possible because it is not allowed to put four queens
on subpanel S28.
If {b4, e2, c7}squares are selected then the squares allowed on subpanel S28 are
g1, h3 and h6. No solution is possible because it is not allowed to put four queens
on subpanel S28.
If {b4, f2, g6}squares are selected then the squares allowed on subpanel S28 are
c1, h3 and c8. No solution is possible because it is not allowed to put four queens
on subpanel S28.
If {b4, f2, g7}squares are selected then the squares allowed on subpanel S28 are
a6, c1, h3, e8 and c8. To satisfy the constraints for placing four queens on subpanel
S28, the alternatives are {a6, c1, h3, c8},{a6, c1, h3, e8}and {a6, h3, e8, c8}.
If {a6, c1, h3, c8}is selected there is not solution because a queen placed in c8
square would be attacking to a queen placed in a6 or c1 squares. If {a6, c1, h3,
e8}is selected there is a solution because none of the queens is attacking another
queen. If {a6, h3, e8, c8}is selected no solution is possible because a queen placed
in e8 square would be attacking to a queen placed in c8.
If {b4, f2, c7}squares are selected then the squares allowed on subpanel S28 are a6,
h3, h6 and e8. No solution is possible because it is not allowed to put four queens
on subpanel S28.
If {b4, g6, c7}squares are selected then the squares allowed on subpanel S28 are
f1 and h3. No solution is possible because it is not allowed to put four queens on
subpanel S28.
Sub-case 1.2: There is one queen on subpanel S12 and there are two queens on sub-
panel S20. (See Table 2)
The following constraints can be conﬁgured:
There is one queen on subpanel S4and it must be placed in d5 square.
There is one queen on subpanel S12.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 65
Figure 5: Subcase 1.1. Graphical representation of the several alternatives for placing
four queens on subpanel S28.
There are two queens on subpanel S20 and one must be placed in b4 square.
There are four queens on subpanel S28. So, it is not allowed to place any queen in
a1, a8, h1 and h8 squares.
Chessboard reconstruction from subpanel S4is represented in Figure 6. The squares
allowed on subpanel S12 are e3 and f6, and the squares allowed on subpanel S20 are
c2, e2, f2, g3, g6, g7 and c7.
Figure 6: Sub-case 1.2. Squares allowed on sub-panels S12 y S20.
To satisfy the constraints for placing one queen on subpanel S12, the alternatives are
e3 or f6 squares. Figure 7 represents the two alternatives.
If e3 square is selected on subpanel S12 , then the squares allowed on subpanel
S20 are c2, g6, g7 and c7. To satisfy the constraint for placing two queens on
subpanel S20, the alternatives are {b4, c2},{b4, g6},{b4, g7}or {b4, c7}squares.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
66 An alternative algorithm for the n–Queens puzzle
Figure 7: Sub-case 1.2. Graphical representation of two alternatives for placing one
queen on subpanel S12.
Since b4 is selected by our constraints, two queens must be placed in a6 and c8
squares. No solution is possible because it is not allowed to put four queens on
subpanel S28. Figure 8 (left hand) represents this alternative.
If f6 square is selected on subpanel S12 , then the squares allowed on subpanel
S20 are c2, e2, g3 and c7. To satisfy the constraint for placing two queens on
subpanel S20, the alternatives are {b4, c2},{b4, e2},{b4, g3}and {b4, c7}squares.
Since b4 is selected by our constraints, one queen must be placed in a7 square and
this would force to place a queen in c1 square that would force again to place a
queen in e8 square. So, {b4, g3}squares is the only one alternative. No solution
is possible because it is not allowed to put four queens on subpanel S28. Figure 8
(right hand) represents this alternative.
Figure 8: Sub-case 1.2. Graphical representation of two alternatives. Left hand if e3
square is selected and right hand if f6 square is selected on subpanel S12.
Sub-case 1.3: There are two queens on subpanel S12 and there is one queen on sub-
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 67
panel S20. (See Table 2)
The following constraints can be considered:
There is one queen on subpanel S4and it must be placed in d5 square.
There are two queens on subpanel S12.
There is one queen on subpanel S20 and it must be placed in b4 square.
There are four queens on subpanel S28. So, it is not allowed to place any queen in
a1, a8, h1 and h8 squares.
Chessboard reconstruction from subpanel S4is represented in Figure 9. No solution
is possible because e3 and f6 squares allowed on subpanel S12 must be selected and it
is not allowed to put four queens on subpanel S28.
Figure 9: Sub-case 1.3. Graphical representation.
´
Sub-case 2.1: There is not any queen on subpanel S12 and there are four queens on
subpanel S20. (See Table 2)
The following constraints can be considered:
There is one queen on subpanel S4and it must be placed in d5 square.
There is not any queen on subpanel S12.
There are four queens on subpanel S20 and one must be placed in b4 square. So, it
is not allowed to place any queen in b2, b7, g2 and g7 squares. (g7 is not allowed
by the constraint to have exactly 4 queens on S20).
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
68 An alternative algorithm for the n–Queens puzzle
There are three queens on subpanelS28. So, at least a queen must be placed in a1,
a8, h1 or h8 squares.
Chessboard reconstruction from subpanel S4is represented in Figure 10. The squares
allowed on subpanel S20 are c2, e2, f2, g3, g6 and c7. To satisfy the constraint for placing
four queens on subpanel S20, the placing of c7 and g6 already prevents us from ﬁnding
a solution. Indeed no solution is possible because it is not allowed to put three queens
on subpanel S28.
Figure 10: Sub-case 2.1. There is not solution.
Sub-case 2.2: There is one queen on subpanel S12 and there are three queens on
subpanel S20. (See Table 2)
The following constraints can be considered:
There is one queen on subpanel S4and must be placed in d5 square.
There is one queen on the subpanel S12.
There are three queens on subpanel S20 and one must be placed in b4 square.
There are three queens on subpanel S28. So, at least a queen must be placed in a1,
a8, h1 or h8 squares.
Chessboard reconstruction from subpanel S4is represented in Figure 11. The squares
allowed on subpanel S12 are e3 and f6, and the squares allowed on subpanel S20 are
c2, e2, f2, g3, g6, g7 and c7.
To satisfy the constraints for placing one queen in the subpanel S12, the alternatives
are e3 or f6 squares. Figure 12 represents these two alternatives.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 69
Figure 11: Sub-case 2.2. Squares allowed on sub-panels S12 y S20.
Figure 12: Sub-case 2.2. Graphical representation of subpanel S12 alternatives.
If e3 square is selected on subpanel S12 , then the squares allowed on subpanel
S20 are c2, g6, g7 and c7. To satisfy the constraint for placing three queens on
subpanel S20, the alternatives are {b4, c2, g7}and {b4, g6, c7}squares.
If {b4, c2, g7}squares are selected then the squares allowed on subpanel S28 are
f1 and a6. No solution is possible because it is not allowed to put three queens on
subpanel S28.
If {b4, g6, c7}squares are selected then the squares allowed on subpanel S28 are
a1, f1 and h8. No solution is possible because it is not allowed to put three queens
on subpanel S28.
Figure 13 represents these alternatives.
If f6 square is selected on subpanel S12 then the squares allowed on subpanel S20
are c2, e2, g3 and c7. No solution is possible because it is not allowed to put three
queens on subpanel S28. Figure 14 represents this alternative.
Sub-case 2.3. There are two queens on subpanel S12 and there are two queens on
subpanel S20. (See Table 2)
The following constraints can be conﬁgured:
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
70 An alternative algorithm for the n–Queens puzzle
Figure 13: Sub-case 2.2. Graphical representation of subpanel S20 and subpanel S28
alternatives when e3 square is selected in subpanel S12.
Figure 14: Sub-case 2.2. Graphical representation of subpanel S20 and subpanel S28
when f6 square is selected in subpanel S12.
There is one queen on subpanel S4and must be placed in d5 square.
There are two queens on subpanel S12.
There are two queens on subpanel S20 and one must be placed in b4 square.
There are three queens on subpanel S28. So, at least a queen must be placed in a1,
a8, h1 or h8 squares.
Chessboard reconstruction from subpanel S4is represented in Figure 15. The squares
allowed on subpanel S12 are e3 and f6. The placing of d5 blocks a8 and h1 and the
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 71
mandatory choice of f6 in S12 blocks the other two corners. Indeed no solution is possible
because it is not allowed to put three queens on subpanel S28.
Figure 15: Sub-case 2.3. Graphical representation of subpanel S28. It is not allowed to
place at least one queen in a1, h1, a8 or h8 squares.
Sub-case 2.4. There are three queens on subpanel S12 and there is one queen on
subpanel S20. (See Table 2)
The following constraints can be considered:
There is one queen on subpanel S4and must be placed in d5 square.
There are three queens on subpanel S12.
There is one queen on subpanel S20 and must be placed in b4 square.
There are three queens in the subpanel S28. So, at least a queen must be placed in
a1, a8, h1 or h8 squares.
Chessboard reconstruction from subpanel S4is represented in Figure 16. The squares
allowed on subpanel S12 are c3, e3, f4 and f6. No solution is possible because it is not
allowed to put three queens on subpanel S12.
Figure 16: Sub-case 2.4. There is not solution.
Finally, two solutions have been found. They are represented in Figure 17.
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
72 An alternative algorithm for the n–Queens puzzle
Figure 17: Solutions of the 8–Queens completion puzzle.
References
[GJN17] Ian P. Gent, Christopher Jeﬀerson and Peter Nightingale. “Complexity of n–
Queens Completion.” Journal of Artiﬁcial Intelligence Research Vol. 59 (2017): 815–
848 DOI: https://doi.org/10.1613/jair.5512
[CMA21] Clay Mathematis Institute. “P vs NP Problem.” Accessed 21 September, 2021.
https://www.claymath.org/millennium-problems/p-vs-np-problem
[Coo71] Stephen A. Cook. “The complexity of theorem proving procedures.” Proceed-
ings Third Annual ACM Symposium on the Theory of Computing, 1971.
[Sch15] Fred Schuh. The Master book of mathematical recreations. Translated by F.
obel. New York: Dover Publications, 1968: 34–346
[BSR08] Roman Bart´ak, Miguel A. Salido and Francesca Rossi. “Constraint Satisfaction
Techniques in Planning and Scheduling.” Journal of Intelligent Manufacturing 21,
(2010): 5–15 DOI: https://doi.org/10.1007/s10845-008-0203-4.
[BSR10] Roman Bart´ak, Miguel A. Salido and Francesca Rossi. “New
Trends in Constraint Satisfaction, Planning, and Scheduling: A Sur-
vey.” The Knowledge Engineering Review 25 (3) (2010): 249–279
DOI: https://doi.org/10.1017/S0269888910000202
[ACP68] Donald E. Knuth. The Art of Computer Programming, Volume 4, Fascicle 5:
Mathematical Preliminaries Redux; Introduction to Backtracking; Dancing Links.
[ACJ18] elia Amado, Susana Carreira and Keith Jones. Broadening the Scope of Re-
search on Mathematical Problem Solving. Springer, 2018: 551–552
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003
David Luque Sacaluga 73
[PrE17] Thomas B. Preußer and Mathias R. Engelhardt. “Putting Queens in Carry
Chains, No´z27.” Journal of Signal Processing Systems, 88(2). (2017):185-–201.
DOI: https://doi.org/10.1007/s11265-016-1176-8
[AY89] B. Abramson and M. Yung. “‘Divide and conquer under global constraints: a
solution to the n–queens probelm,” Journal of Parallel and Distributed Computing
6(3), (1989): 649–662
[BS09] J. Bell and B. Stevens. “A survey of known results and re-
search areas for n–queens.” Discrete Mathematics, 309 (2009): 1-–31.
DOI: http://dx.doi.org/10.1016/j.disc.2007.12.043
[BD75] A. A. Bruen and R. Dixon. “The n-queen problem.” Discrete Mathematics, 12,
(1975): 393—395. DOI: https://doi.org/10.1016/0012-365X(75)90079-5
[BM02] A. P. Burger and C. M. Mynhardt. “An upper bound for the minimum number
of queens covering the n×nchessboard.” Discrete Applied Mathematics 121, (2002):
51—60. DOI: https://doi.org/10.1016/S0166-218X(01)00244-X
[Eng07] M. R. Engelhardt. “A group-based search for solutions of the
n-queens problem.” Discrete Mathematics 307, (2007): 2535—2551.
DOI: https://doi.org/10.1016/j.disc.2007.01.007
[ET92] C. Erbas, S. Sarkeshik and M. M. Tanik, “Diﬀerent perspectives of the N
queens problem.” Proceedings ACM Computer Science Conference (1992): 99-–108.
DOI: https:/doi.org/10.1145/131214.131227
[Ber91] Bo Bernhardsson. “Explicit Solutions to the N–Queens Problem for All N.” ACM
SIGART Bulletin 2 (2) (1991). DOI: https://doi.org/10.1145/122319.122322
[GJ79] Michael R. Garey and David S. Johnson. Computers and Intractability: A Guide
to the Theory of NP-Completeness. W. H. Freeman, 1979. ISBN 978-0-7167-1045-5
[CMA21] Clay Mathematis Institute. “The 8-Queens Puzzle.” Accessed 21 September,
2021. https://www.claymath.org/events/news/8-queens-puzzle
Recreational Mathematics Magazine, pp. 39–72
DOI 10.2478/rmm-2021-0003