Puzzle—Queen Puzzles: An Educational Approach to Integer Programming Techniques

Published Online:https://doi.org/10.1287/ited.2024.0122

Abstract

Chessboard puzzles involving queens are valuable teaching examples for integer programming. We present the queens domination and peaceable queens problems in an educational format and describe our classroom experience with teaching these problems. We discuss techniques for improving efficiency, such as symmetry breaking, valid inequalities, bounds, and parameters.

Funding: This work was supported by the Australian Government (RTP Scholarship).

1. Introduction

Logic puzzles are a great introduction to linear and integer programming (IP). They make for fun problems to solve and can be used to teach introductory and more advanced modelling techniques. Recent examples of integer programming formulations to solve puzzles include the snake eggs puzzle (Harris and Forbes 2023) and the fillomino puzzle (Pearce and Forbes 2017). These two examples give an introduction to some advanced techniques such as Benders decomposition, lazy constraints, and row/column generation. In common with many logic puzzles, they do not have an objective .

Chess puzzles are also a popular choice to solve with integer programming. Chess puzzles often have an objective function, which can make them more interesting and instructive. Examples include the chess avoidance puzzle, in which the aim is to place a set of chess pieces on a board so that none attack each other (Chlond 2015), and the knights exchange puzzle, in which the aim is to find the minimum number of moves to swap the black and white knights placed in the corners of a 3×3 chessboard (Iranpoor 2021).

Queens and knights stand out as the most intriguing pieces, showcasing distinctive mobility compared with other chess pieces. In this paper, chess problems involving queens will be modelled and solved using integer programming. The first of these problems is queens domination, in which the aim is to use the smallest number of queens to attack all the squares on the chessboard. The second is peaceable queens, where we find the largest size for two nonattacking armies of queens on a given chessboard.

These problems make for excellent teaching examples, as they are easy to describe and have the potential for extensions and alternative models. Our main objectives when teaching these problems are to provide interesting modelling examples, cover strategies for improving integer programming efficiency, and provide examples of how to report performance. The problems are taught in a third-year mathematics course for advanced operations research at the University of Queensland.

All problems are solved on a queens graph. For further information on graph theory, refer to West (2001). The queens graph, Qn, corresponds to the n×n chessboard. The vertex set is each of the n2 squares on the chessboard, and an edge exists between two vertices if a queen can move between the corresponding squares in one move. We say that a square is attacked if there is an edge between it and a queen.

The models in this paper are implemented and taught using the commercial solver Gurobi (Gurobi Optimization 2024). Gurobi is freely available to teachers and students for academic purposes. The features discussed later, including solver parameters and callbacks, specifically refer to Gurobi. A comparable solver, CPLEX, offers similar functionality and is also freely accessible for academic use.

In each of the next two sections, we describe a specific problem: queens domination and peaceable queens. Each problem will be described in detail and then formulated as an integer program. Further alternative methods for solving the problem will then be investigated, along with other optimisation techniques. Finally, results for each problem will be compared with different approaches. We finish with some concluding remarks.

2. Queens Domination Problem

Domination puzzles on chessboards are a traditional problem that can be formulated with integer programming (Chlond and Toase 2002). The aim of these domination puzzles is to find the minimum number of pieces to put on a chessboard so that every square is attacked. Chlond (2010) looked at the knights domination problem using integer programming and a spreadsheet approach and, interestingly, considered extending the problem to a chessboard on any two-dimensional surface.

The queens domination problem is simple to understand. On an n×n chessboard, what is the minimum number of queens needed to attack every square? This is the general case, but the case where queens are independent of each other is often considered. Two solutions to the queens domination problem on the 8×8 chessboard are shown in Figure 1, with the general case on the left and the independent case on the right.

Figure 1. 8×8 Chessboard with Dominating Queens
Notes. (a) General case. (b) Independent case.

This problem is linked to dominating and independent sets in graphs. Let G=(V,E) be a graph. A subset of the vertex set, SV, is a dominating set if every vertex not in S is adjacent to a vertex in S. The domination number, γ(G), is the size of the minimum dominating set of graph G. An independent set in a graph is a set of pairwise nonadjacent vertices. The size of the minimum independent dominating set of graph G is denoted i(G). The minimum number of queens needed to attack every square is then the domination number. γ(Qn) for the general case and i(Qn) for the independent case.

This problem has been addressed before. Weakley (2018) provides an overview of work in the past 25 years. Bounds have been found on the domination number through a variety of combinatorial techniques.

The On-Line Encyclopaedia of Integer Sequences (OEIS) has a sequence for the queens domination number (A075458) and the independent domination number (A075324) (Sloane 2024). These can be seen in Tables 1 and 2.

Table

Table 1. Domination Number

Table 1. Domination Number

N456789101112131415
γ(Qn)233455556789
N1617181920212223242526
γ(Qn)9991011111212131313–14
Table

Table 2. Independent Domination Number

Table 2. Independent Domination Number

N4567891011121314151617
i(Qn)33445555778999
N1819202122232425262728293031
i(Qn)1011111112131313141515161617

Parra Inza et al. (2024) use integer programming to find a dominating set for any graph, not specifically the queens graph. Their formulation is used as the base formulation in this paper. They also have created heuristic and exact algorithms that solve the problem faster in some cases.

2.1. Base Formulation

Consider a size n chessboard. Let N={1,,n}, and let S be the set of n×n squares on the chessboard. Each element in S is a pair, (s0,s1),s0N,s1N, that describes the row and column position. The adjacency matrix A has entries As,t=1 if a queen can move from square s to t or s=t, and zero otherwise. The binary variables xs are equal to one if a queen is placed on square sS, and zero otherwise.

The queens domination problem can be formulated as an integer programming problem as follows:

minimisesSxs(1)
subject totSAs,txt1,sS.(2)

The Objective Function (1) minimises the number of queens. Constraints (2) ensure that every square has at least one queen adjacent to it in the graph theoretic sense of adjacent; that is, every square is attacked or occupied by a queen.

2.2. Independence

To extend the problem to independent queens, the following constraints are added to the base formulation:

sS|s0=ixs1,       iN(3)
sS|s1=jxs1,   jN(4)
sS|s0s1=kxs1,       k(N,N)(5)
sS|s0+s1=kxs1,   k2(N1).(6)

Constraints (3) ensure that there is no more than one queen in any row. Similarly, Constraints (4) ensure that there is no more than one queen in any column, whereas Constraints (5) and (6) do the same for the diagonals. This is a stronger version of independence constraints, which are specific to queens on a chessboard. Independence can be ensured more generally with the following constraint:

tS|As,t=1xtMs(1xs),  sS,
where Ms is the number of vertices adjacent to s.

2.3. Feasibility Approach

Let q be a potential domination number. In many cases of the queens domination problem, it is known that the solution may be q or q+1. When this is the case, a feasibility problem can be used to find whether a solution exists. From the base formulation, the objective is modified to potentially speed up this process, and constraints are added. The new objective and constraint are as follows:

maximisesS(xs×tSAs,t)(7)
subject to (2),sSxs=q.(8)

The objective function (7) aims to maximise the number of squares that are being attacked to speed up finding a feasible solution. Constraint (8) forces the solution to have q queens. Constraints (2) are still used to ensure every square is attacked.

2.4. Symmetry Breaking

An approach to speed up the model is symmetry breaking, where we want to break the symmetries of rotation and reflection. This can be done by adding variables and constraints to force parts of the board to have more queens than others. Let q1,q2,q3,q4 be integer variables that denote the number of queens in quadrants 1, 2, 3 and 4, respectively. Refer to Figure 2 for a visualisation of these regions.

Figure 2. Regions of the Chessboard

Let H1 be the first half of N, that is, the set {0,1,,N1/2}, and H2 be the second half, that is, {N1/2+1,,N}. The following constraints are added to the formulation to break symmetry:

q1=sS|s0H1,s1H1xs,(9)
q2=sS|s0H2,s1H1xs,(10)
q3=sS|s0H1,s1H2xs,(11)
q4=sS|s0H2,s1H2xs,(12)
q1q2,(13)
q1q3,(14)
q1q4,(15)
q2q3.(16)

Constraints (9)(12) count the number of queens in each specified region to populate the new integer variables. Constraints (13)(16) break symmetry by forcing areas of the board to have at least as many queens as other areas.

2.5. Valid Inequalities

The next speedup we consider is valid inequalities—note this is only relevant for the independent case. Valid inequalities are constraints that are implied by the integer solution but may cut off solutions to linear relaxations of the problem. They potentially reduce solution time by cutting solutions earlier in the branch-and-bound tree. In this problem, if the solution has more than one queen in particular sets of squares, then the solution is cut, as it does not satisfy independence. These sets are the four corners of any-sized square within the board, plus the centre square if it exists, and the four corners of any-sized diamond within the board, plus the centre. These two sets can be seen in Figure 3.

Figure 3. Valid Inequality Visualisation

Let the set of squares in Figure 3(a) be V1 and the set of squares on the right be V2. Then, we add the following constraints when there is more than one square used in one of these sets:

sV1xs1,V1SsV2xs1,V2S.

We use the callback feature of modern solvers to do this. As an example, for n=8, we find a state where squares (2,2), (5,2) and (2,5) have variable values of 0.5, 0.375 and 0.25, respectively; see Figure 4 for the solution at this state. Because these form part of a square and their values add up to greater than one, the valid inequality constraint is violated, and this solution is cut off in the callback.

Figure 4. Valid Inequality Violation Visualisation

2.6. Bounds and Parameter Tuning

The next speedup that was implemented was the use of lower and upper bounds. The values for lower and upper bounds are taken from Table 1. Let L be the lower bound. Then, the following constraint ensures that there are at least as many queens as the lower bound:

sSxsL.

The upper bound is used as a cutoff value, so the model will only look for solutions where the objective value is no worse than the upper bound.

Finally, we experimented with other solver parameters in Gurobi (Gurobi Optimization 2024). The parameters that could potentially make an impact were BranchDir, MIRCuts and MIPFocus. We set BranchDir = 1, forcing the solver to explore the up branch first. We set MIRCuts = 2, so the solver adds mixed-integer rounding cuts more aggressively. We set MIPFocus = 2 to force the solution strategy to focus on finding an optimal solution.

2.7. Student Experience

Students found this problem straightforward and easy to follow. They suggested using a big M constraint for independence but quickly grasped the tightness of Constraints (3)(6). They also proposed several insightful efficiency improvements. In particular, they identified similar ideas for symmetry breaking (force a queen to be in the top left corner) and valid inequalities (cutoff solutions with more than one queen in a row). These suggestions provided a natural segue into discussing the symmetry-breaking techniques and valid inequalities used in our formulation. Additionally, students considered partitioning the board into smaller sections for larger instances to further reduce symmetry.

When asked to predict the most impactful technique, students expected symmetry breaking to have the greatest effect on computation time. They were surprised by the actual results (Figure 5 in the following section). We also discussed how to compare these methods, particularly in cases where performance differences between individual runs are minor. Students gained an appreciation for percentage solved versus time graphs as a useful tool for such comparisons.

Figure 5. Percentage Solved vs. Time, Different Approaches on a 14×14 Chessboard for the General Case

2.8. Results

In this section, models with the different speedups are compared with the base model. The results in Tables 1 and 2 have been verified up to a point.

Students are often surprised to learn that integer programming solvers are chaotic. That is, even very small changes in the specification of the model, for example, reordering of constraints, can lead to completely different solution trajectories and runtimes. Therefore, to investigate the effectiveness of the different approaches and speedups, each approach needs to be tested with many different values of the solver parameter Seed, which changes the solution trajectory. This is done separately for the general and independent cases. We compare the following approaches for the general case:

  • base formulation

  • base + symmetry

  • base + upper and lower bounds

  • base + branching

  • base + mixed-integer rounding (MIR) cuts

  • base + mixed-integer programming (MIP) focus

The 14 × 14 chessboard was solved with 1,000 different seeds for each solution approach. The models were programmed in Python 3.11.7 via the Anaconda distribution 23.11.0 and solved using Gurobi 11.0.1 (Gurobi Optimization 2024). Jobs were run on a computing cluster with 2.5 GHz CPUs. Figure 5 shows the percentage of these 1,000 instances that are solved against runtime for each of the different approaches. We can see that the two approaches that speed up the base formulation are adding in lower and upper bounds and setting the branching direction. The graphs also demonstrate the variability in run time for the same approach when only the seed is varied. Table 3 displays the minimum, maximum, mean and standard deviation of the runtimes for the different approaches. The same process was carried out to compare the independent case with and without valid inequalities. It was determined that the valid inequalities do not speed up the program, as the independent case had an average runtime of 76 seconds compared with 398 seconds with the addition of valid inequalities. It is likely that Gurobi already implements symmetry breaking and valid inequalities on its own, so the manual addition of these is not helpful. For teaching purposes, this can be run prior to the lesson and presented in class. For smaller examples, students can divide and conquer problems using their laptops.

Table

Table 3. Comparing Timing of Approaches on a 14 × 14 Chessboard for General Case

Table 3. Comparing Timing of Approaches on a 14 × 14 Chessboard for General Case

TimesBaseSymmetryBoundsBranchDIRMIRCutsMIPFocus
Minimum198.39403.109.06148.50248.42267.18
Maximum230.13511.9912.54170.79294.53323.04
Mean209.10463.9511.59159.63262.25295.01
Standard deviation9.4921.010.665.3513.1014.23

3. Peaceable Queens Problem

Ainley (1977) first introduced the peaceable queens problem in his book of mathematical puzzles. This appeared alongside many other chess puzzles. Bosch (1999) posed this as an integer programming problem.

The goal of this problem is to find the largest size, m(n), of two equal sized armies of queens on an n×n chessboard so that queens from opposing armies cannot attack each other. A solution for the 8×8 chessboard can be seen in Figure 6. The peaceable queens problem can demonstrate the effect of different integer programming formulations of the same problem, which makes a valuable teaching example.

Figure 6. Peaceable Queens on an 8×8 Chessboard

In graph theory, a vertex colouring on a graph is an assignment of colours to vertices in the graph with some conditions. Peaceable armies on a graph correspond to a vertex colouring so that no two adjacent vertices have different colours.

Peaceable queens is in the OEIS as A250000 (Sloane 2024) with solutions for 1n15; these values can be seen in Table 4.

Table

Table 4. Maximum Peaceable Army Sizes

Table 4. Maximum Peaceable Army Sizes

N123456789101112131415
m(n)0012457912141721242832

Smith et al. (2004) have also presented a constraint programming formulation along with a tighter version, which uses row and column variables. These ideas are used in this paper’s formulation of the problem.

3.1. Formulation

We use the same notation as previous sections. Additionally, let M be the maximum number of squares that a queen can attack, namely, 4(n1). The binary variables xs are equal to one if a white queen is placed on square sS, and zero otherwise. The binary variables ys are equal to one if a black queen is placed on square sS, and zero otherwise.

The peaceable queens problem can be formulated as an integer programming problem as follows:

maximisesSxs(17)
 subject tosSxs=sSys,(18)
tS|As,t=1xtM(1ys),sS(19)
tS|As,t=1ytM(1xs),sS.(20)

The Objective Function (17) maximises the number of queens in the white army. Constraint (18) ensures that the armies are the same size. Constraints (19) and (20) ensure that black and white queens cannot attack each other.

3.2. Tighter Formulation

We can tighten the formulation considerably as follows. Add binary variables ri, cj, dk, el, which are equal to one (zero) if the relevant row, column or diagonal can be occupied by a white (black) queen.

The peaceable queens problem can be formulated as a tighter integer programming problem as follows:

maximisesSxs(21)
subject tosSxs=sSys,(22)
xi,jri,i,jN(23)
yi,j1ri,i,jN(24)
xi,jcj,i,jN(25)
yi,j1cj,i,jN(26)
xi,jdij,i,jN(27)
yi,j1dij,i,jN(28)
xi,jei+j,i,jN(29)
yi,j1ei+j,i,jNi,jN.(30)

Constraints (23)(30) ensure that each row, column or diagonal, respectively, is restricted to be occupied by either white or black.

3.3. Student Experience

Students understood this problem and its formulations without difficulty. After being prompted for ideas for a faster formulation, the students suggested composite variables and column generation. This was a great suggestion, particularly in following with the content of the course. However, it is not obviously applicable in this context.

Within the tighter formulation, we experimented with changing parameters such as BranchPriority, MIPFocus and Cuts. To determine if any of these changes were beneficial for efficiency, we used the Wilcoxon and Mann–Whitney U tests. The results of a Mann–Whitney U test applied to 10 different seeds for n=9 showed that adding branching priority to the row and column variables was faster, with a p-value of 0.028. This demonstrated to students a meaningful way to compare timings of approaches.

3.4. Results

In this section, we present the results of the peaceable queens problem. Table 5 presents solution values m(n) and their runtimes (in seconds) for n=4,,16, using the initial and tighter formulations, respectively. This can also be seen graphically in Figure 7. Note that the tighter formulation for n=16 was given a time limit larger than 48 hours. It is clear that the tighter formulation outperforms the initial formulation, which is notable because the tighter formulation has a larger number of variables and constraints. For example, the 9×9 chessboard has 210 variables and 641 constraints in the tighter formulation, whereas the initial has only 162 variables and 163 constraints.

Table

Table 5. Peaceable Queens Timing Results

Table 5. Peaceable Queens Timing Results

Nm(n)Initial(s)Tighter(s)
421≤1
5411
6532
77154
891317
9121,13834
10149,237109
1117119,514302
1221≥48 hours902
1324≥48 hours3,645
1428≥48 hours31,287
1532≥48 hours110,407
1637≥48 hours3,430,623


Note. The result in bold is a new confirmed optimal solution.

Figure 7. Peaceable Queens Approaches: Log Time vs. n

4. Conclusion

In this paper, many instances of queens domination and peaceable queens have been solved using integer programming. Initial approaches and upgrades have been stepped through in a manner suitable for educational purposes. The upgrades introduced have demonstrated improvements in computational efficiency. These findings underscore the value of queens problems as teaching tools in optimisation. There is potential for further exploration in more graph theory and combinatorial problems.

Acknowledgments

We extend our gratitude to Lachlan W. J. McBeath for his contributions to the figures in this paper. We thank the operations research students for their valuable participation in the discussion of these problems. We also thank the editors, reviewers and everyone involved for their thoughtful consideration of our work.

References

  • Ainley S (1977) Mathematical Puzzles (G. Bell & Sons Ltd., London).Google Scholar
  • Bosch RA (1999) Peaceably coexisting armies of queens. Optima: Math. Programming Soc. Newsletter 62:6–9.Google Scholar
  • Chlond MJ (2010) Puzzle—Knight domination of 2-D surfaces: A spreadsheet approach. INFORMS Trans. Ed. 10(2):98–101.LinkGoogle Scholar
  • Chlond MJ (2015) Puzzle—Chess avoidance puzzles. INFORMS Trans. Ed. 15(3):254–256.LinkGoogle Scholar
  • Chlond MJ, Toase CM (2002) IP modeling of chessboard placements and related puzzles. INFORMS Trans. Ed. 2(2):1–11.LinkGoogle Scholar
  • Gurobi Optimization (2024) Gurobi optimizer reference manual. Accessed July 24, 2025, https://docs.gurobi.com/projects/optimizer/en/current/index.html.Google Scholar
  • Harris M, Forbes M (2023) The snake eggs puzzle: Preparing students for Benders decomposition. INFORMS Trans. Ed. 23(3):210–217.LinkGoogle Scholar
  • Iranpoor M (2021) Knights exchange puzzle—Teaching the efficiency of modelling. INFORMS Trans. Ed. 21(2):108–114.LinkGoogle Scholar
  • Parra Inza E, Vakhania N, Sigarreta Almira JM, Hernández Mira FA (2024) Exact and heuristic algorithms for the domination problem. Eur. J. Oper. Res. 313(3):926–936.CrossrefGoogle Scholar
  • Pearce RH, Forbes MA (2017) Puzzle—The fillomino puzzle. INFORMS Trans. Ed. 17(2):85–89.LinkGoogle Scholar
  • Sloane NJA (2024) On-line encyclopedia of integer sequences. Accessed July 24, 2025, https://oeis.org/.Google Scholar
  • Smith BM, Petrie KE, Gent IP (2004) Models and symmetry breaking for ‘peaceable armies of queens’. Régin JC, Rueher M, eds. Integration AI OR Techniques Constraint Programming Combin. Optim, Problems. CPAIOR 2004, Lecture Notes in Computer Science, vol. 3011 (Springer, Berlin, Heidelberg), 271–286.Google Scholar
  • Weakley WD (2018) Queens around the world in twenty-five years. Gera R, Haynes T, Hedetniemi S, eds. Graph Theory, Problem Books in Mathematics (Springer International Publishing, Cham, Switzerland), 43–54.CrossrefGoogle Scholar
  • West D (2001) Introduction to Graph Theory, Featured Titles for Graph Theory (Prentice Hall, Hoboken, NJ).Google Scholar