# Greatest number of non-attacking moves that queens can make on an $n \times n$ chess board.

I'm trying to extend my OEIS sequence A275815: Maximum total number of possible moves that any number of queens of the same color can make on an $$n \times n$$ chessboard.

I have computed the first five terms by brute force, and examples of each are given below.

For $$n \geq 6$$, Alec Jones has conjectured that $$A275815(n) = 8(n-2)^2$$, which is achieved by placing $$4n - 4$$ queens around the border of the board—but this has not been proven. Alec's heuristic is based on summing the number of queens that can move to each empty square (which is no more than 8) instead of summing the number of moves each individual queen can make.

Any thoughts on how to begin to prove this conjecture?

Examples:

A $$2\times 2$$ chess board can "host" 4 queen moves:

1. $$a1 \to b1$$
2. $$a1 \to b2$$
3. $$a2 \to b1$$
4. $$a2 \to b2$$ A $$3 \times 3$$ board can host 17 moves: (The queen at a3 has three moves, the queen at a2 has four moves, and the queens at b1 and c3 each have five moves.) A $$4 \times 4$$ board can host 40 moves: And a $$5 \times 5$$ board can host 76 moves: • What is a non-attacking move?? Is that a typo for non-capturing move?
– bof
Dec 25, 2016 at 0:41
• I wrote a new lines of code, and using a "somewhat goal-directed" (but still brute-force-ish) search, it seems that the statement is right (but of course this is even less a proof than the statements in the OEIS that sound "convincing"). Did you consider an exhaustive brute-force search for the case of 6x6? (It's somehow a pity that the first 5 steps produce so interesting patterns, and the remaining ones should all just consist of a filled border...) Dec 25, 2016 at 2:39
• @Marco13 I truly brute forced up to the $5 \times 5$ board—all $2^25$ configurations of queens. A $6 \times 6$ board is just too big for such a naive computation. Dec 25, 2016 at 3:28
• @bof, "non-capturing" would work fine too. Feel free to edit if you think the question is ambiguous. Dec 25, 2016 at 3:30
• Yes, 6x6 may already be too large, but think that it could still brute-forced, with an efficient implementation (exploiting symmetries and rotations), and a few hours of time. However, it may not be worth the effort, once someone comes up with a rigorous proof. (I did some "greedy" search implementation, and it quickly finds the "border-pattern" for larger sizes (>20), but of course, this has nothing to do with a "proof" ). Dec 25, 2016 at 12:21

Here’s a proof/sketch for sufficiently large n. I think $n \geq 4 * 5 + 3 = 23$ for operation (c). You may be able to apply the new scoring to the Lemma, so you then only need (c) to be nonnegative, in which case $2(n-2) - 8 \geq n - 1$, so $n \geq 11$. Also, in the new scoring, diagonals are a bonus, unless it's short, so you may be able to get $2(n-2) - 4 \geq n - 1$, or $n \geq 7$. This is my first post, so I hope it's clear.

The main idea is to look at the queens in each row/col/diagonal and to show that if the arrangement doesn’t satisfy certain properties, then we can locally improve the arrangement, by which I mean that we will find a different arrangement that is not worse. The second key idea is that sometimes, it’s hard to locally improve an arrangement. However, you can then overestimate the score (the score is the number of non-attacking moves) for each arrangement so that the best arrangement has the same score, but in a way that each non-maximal arrangement can now be improved.

There are 3 operations we can apply on a queen in a row/column/diagonal, which we call a slice.

(a) It is sandwiched between 2 queens (not necessarily directly) and we delete it.

(b) It is the last/first queen in that slice, we delete it and add it to its end of the slice.

(c) It is the only queen in that slice, and we delete it and put 2 queens on both ends of that slice.

These operations can be applied simultaneously on a single queen along different slices in the natural way.

Lemma. We can assume there is no row or column with only one queen (or else we can improve the arrangement).

Proof. If there is, then we apply operation (c) in that row or column and in every other slice containing the queen, we apply (a), (b), or (c) as appropriate. Note that (c) increases the score for a row/col by an order of n, i.e. from $n-1$ to $2(n-2)-8$. (a) increases by 2, and (b) can only worsen the score by at most 4 (+1 for that slice, and -5 since now queens can’t move into the edge where the queen was pushed to). Since n is sufficiently large, (c) outweighs (a) and (b), so the arrangement is non-worsened.

Proof of theorem. Suppose a diagonal has $k$ squares. Then, if $k = n$, which we call a long diagonal, or $k = 2$, a short diagonal, we score it normally. $k = 1$ is naturally scored 0. For $k = 3$, we ignore the middle of the diagonal when scoring (so having both ends is 2, one end is 2, and no ends is 0). Otherwise, let the number of queens on the ends of that diagonal be $0 \leq q \leq 2$. We score it as $2(k-2) - 2 + q$ instead. Note that the new score is always better or the same than the original score.

Now, suppose we have a queen not at the edge of the board. (a) always increases the score by 2. For (b), we have a few cases.

1. The slice is a row/col, and the end of the slice is not in a long or short diagonal. Then we get +1 along that slice, +2 from the added queen for the diagonals (unless one of them is the k = 3 diagonal, in which case we only get one diagonal, so +1), and at most -2 along the slice for the new queen, for a total of +1 (or + 0).

2. The slice is a row/col and the end of the slice is in a short diagonal and the queen is not on a long diagonal. Then we get +2 along the slice, +1 from the non-short diagonal, potentially -1 from the short diagonal, and -2 along the edge slice, for a total of +0.

3. The queen is on a long diagonal and the end is on a short diagonal. This is a special case where we look at the combination of some of the operations we apply on that queen. Label the corner D, the queen’s square as A, and the other two squares in the 2x2 defined by A and D as B and C. We now cover all the cases for A, B, C, and D. The ending state is B, C, and D are all occupied and A is unoccupied.

• B, C are occupied. We get +2 along B’s slice, +2 for C, and at most -1 for D.
• B, C, D all unoccupied. We get +1 along B’s slice, -1 along the edge slice for B, and +1 for the diagonal B is in (the short BC diagonal doesn’t change). For C, we also get +1 total. For D, we get +1 along the long diagonal, and -2 from the edge slices. Total, we have at least +1.
• B, C unoccupied, D is occupied. We get +1 along B’s slice, -2 for B’s edge, and +1 for the diagonal B is in. Same for C. For D, we get +2, so overall at least +2.
• C occupied, B, D are unoccupied. We get +1 along B’s slice, -1 for B’s edge, +1 for the diagonal B is in, and -1 for the short diagonal. We get +2 for (a) on C, and +1 along the long diagonal and -2 along the edges for D. Total is +1.
• C occupied, B unoccupied, D occupied. We get +1 along B’s slice, -2 for B’s edge, +1 for the diagonal, and -1 for the short diagonal. C is +2 and D is +2. So overall +3.
4. The slice is the long diagonal. We already covered when the queen is one away from the corner in case 3, so now the queen is 2 away. Either way, applying (a) or (b) increases by 2 along the slice, and decreases by at most 2 along the edge slices. So, the total is +0.

With all the cases (note that we didn't need to consider slices that were non-short/long diagonals), we see that we can apply the operations to any queen in the middle to push it to all the edges without worsening the score. But from the Lemma, there’s at least 2 in each row/col, so the whole border except for the corners have queens in them. From there, it is clear that filling in the corners improves the (new) score. So for n sufficiently large, the best is to have all queens along the border, for a total of $8(n-2)^2$ (which has the same score in both systems).

I investigated a similar problem a few years ago as part of my effort to archive the Enigma puzzles from New Scientist magazine.

Enigma 69 (from July 1980) is about determining the maximum number of moves on a 4×4 board, using the minimum number of Queens. The puzzle text implies that the "border configuration" is known to be optimal for an 8×8 board.

I recently revisited this puzzle and refined my algorithm to allow me to fully investigate the 6×6 board, and I can confirm that the maximum number of moves in this case is 128 with 20 Queens (using the "border configuration").

You can see all my results at: [ https://enigmaticcode.wordpress.com/2013/03/10/enigma-69-maximum-queen-moves/ ]

It's worth noting that although 40 moves is indeed the maximal solution for the 4×4 board, it can be achieved with only 6 Queens (it is also achievable with 7 and 8 Queens).

While still being brute force, turning this problem into an integer linear programming problem can fill the gap up to "sufficiently large".

Here is python3 code using the PuLP library (the relevant package is called python3-pulp on debian) that confirms the conjecture for all $$n<25$$ in less than a minute. It can go higher if needed.

from pulp import *

near = [-1, 0, 1]

dirs = [ (x,y) for x in near for y in near if (x,y) != (0,0) ]

def ray(start, dir, valid):
res = []
x,y = start
dx,dy = dir
while x in valid and y in valid:
res.append((x,y))
x += dx
y += dy
return (start,dir,res[1:])

def mq(n):
print("n =", n)
prob = LpProblem("Maximize Queen moves", LpMaximize)
R = range(n)
positions = [(i,j) for i in R for j in R]
queen = LpVariable.dicts("Q", positions, 0, 1, LpInteger)
# queen[pos]=1 iff there is a queen at pos
rays = [ ray(pos, dir, R) for pos in positions for dir in dirs ]
rays = [ r for r in rays if r != [] ]
posdirs = [ (pos,dir) for pos,dir,_ in rays ]
reach = LpVariable.dicts("R", posdirs, 0, 1, LpInteger)
# reach[(pos,dir)]=1 iff pos is free and there is a queen in direction dir
prob += lpSum(reach) # objective function
for pd in posdirs:
prob += queen[pd] + reach[pd] <= 1
for pos,dir,r in rays:
prob += lpSum([queen[p] for p in r]) - reach[(pos,dir)] >= 0
prob.solve()
assert prob.status==LpStatusOptimal, "Couldn't solve"
print(int(value(prob.objective)), "moves:")
print()
for i in R:
for j in R:
print(".Q"[int(value(queen[(i,j)]))], end='')
print()
print()
print()

if __name__ == "__main__":
for i in range(2,25):
mq(i)


If you want to minimize the number of queens as secondary optimization goal, use something like n**2*lpSum(reach)-lpSum(queen) as objective function.

I'm sorry for responding with a non-encompassing answer, however I do have a few observations I'd like to share:

Mr.Jones' conjecture considers most scenarios but I believe by placing a queen in the center (as that location will not be covered by all eight directions) will net more host locations due to the queen covering 2(n-1) - 4 locations (2(n-1) is the diagonals un-covered by the queens from that direction and thus earned, the minus four is due to the queen covering four queens' ability to host on the center, and 2(n-1) > 4 when n > 3 so placing a queen on the center will be better thus the equation should be: 8(n-2)^2 + 2(n-1) - 4 if n is odd (as if not the center is four locations thus the situation is wildly different).

Other things that may help: Relative to the center, any queen placed on a location will net 4(n-1) - 2(x) where x is the distance the queen is from the center.

I'm sorry if I didn't explain this well, I hope it helps.

Edit: I'm not sure if I'm breaking a code here, as this observation is not an answer and more a comment, I can't place a comment due to being a novice and I'm unable to find a way to contact you other than posting here, if I'm out of line please inform me and I'll delete the comment, thank you for your time.

• If you have a partial answer, it may be appropriate to post it if it adds something that no other answer does toward a solution. In this case there are no other answers to compare, so it may be easy to reach that bar. However you could do more with your ideas to address the Question. For example, show how your formulas relate to the smallish cases described by the OP. In general even a partial answer should exhibit an amount of effort commensurate with the effort put forward in the Question. Dec 25, 2016 at 0:51
• In general the strategy of putting a queen in the middle and queens around the edges (except the corners) allows fewer possible moves because the squares along the diagonals only admit 7 moves each in that case. Dec 25, 2016 at 1:43

It may be useful to first determine the optimal number of queens, represented by the variable Q. Right now the optimal number of queens Q follows the sequence 2, 4, 8, 11 for n= 2, 3, 4, 5.

It's also interesting that each board where n is odd contains a subset of the previous board where n is odd. This is also true for the evens. Although there is no reason to believe this will continue, I have a hunch that it will.

If you can't brute force a solution for n, maybe you could guess an optimal pattern for n based on the solution of n-2, and check it via brute forcing for a similar number of queens to see if there are any better ones.

• Er, I changed my mind about the odds/evens subset rule. I think the sixth board will look like four 3-boards smushed together in a way that resembles how the 4-board was made up of 2-boards, i.e. with each 3-board being rotated 90 degrees. Dec 25, 2016 at 7:16