Number of ways of placing $4$ knights on a $4 \times 4$ board such that for every knight, there is an unique knight attacking it

218 Views Asked by At

I extracted this problem from an online contest. On the day of the contest (while it was still accepting responses), someone posted this very question on this site. It was closed because of lack of context (The OP just copy-pasted the exact wording of the question in the title and the body). I can't find that post at the moment because it has been deleted probably.

Find the number of ways of placing $4$ knights on a $4 \times 4$ board such that for every knight, there is a unique knight attacking it.

I spent a lot of time attempting to approach this problem combinatorically, but I couldn't make it.

The knight attacks in an L-shape and can hop over other pieces. So if a knight is placed on the square $(i, j)$ (read as $i$-th row and $j$-th column), it attacks these $8$ squares $(i\pm 2, j\pm1)$, $(i\pm 2, j\mp 1)$, $(i\pm 1, j\pm 2)$, $(i\pm 1, j\mp 2)$. Of course, if the knight is placed towards the corner, some of these squares wouldn't exist.

Using brute force, I found that out of the $\binom{16}{4}=1820$ ways of placing the knights, $128$ of them satisfy the required condition i.e., each knight is uniquely attacked by another (if there's no mistake in my code).

I also wonder if this problem can be generalized to finding the number of ways of placing $k$ knights on an $n\times n$ board such that for every knight, there is a unique knight attacking it. I believe that there should be some recurrence relation (which I am unable to establish) and hence a generating function.


Summing up the comments, here's what my code shows:

  • $5$ knights, $5\times 5$ board makes zero ways. (It's zero for odd number of knights)

  • $6$ knights, $6\times 6$ board makes $10304$ ways. (It took over a minute to find this.)

  • $4$ knights, $5\times 5$ board makes $520$ ways.

I am sharing the code (Python 3) as well which is of course, grossly inefficient... You need as many for loops as knights :(

n=int(input("Dimension of the board, n = "))
k=int(input("Number of knights, k = "))
p = [(i,j) for i in range(n) for j in range(n)]

ad = [(1,2), (2,1), (1,-2), (-2,1)]
ad += [(-i, -j) for i, j in ad]

t=0
n*=n

code = "i=[-1]+[0]*k\n"
indent = ""
for j in range(1, k+1):
    code += indent + f"for i[{j}] in range(i[{j-1}]+1, n):\n"
    indent += " "
code += indent + "knights={p[j] for j in i[1:]}\n"
code += indent + f"for j in i[1:]:\n{indent} placed=p[j]\n{indent} attacked=set(map(lambda x: (x[0]+placed[0], x[1]+placed[1]), ad))\n{indent} if len(attacked.intersection(knights))!=1:\n{indent}  break\n{indent}else: \n{indent} t+=1\nprint(t)"
#print(code)
exec(code)
1

There are 1 best solutions below

0
On

Let's investigate three cases based on symmetry.


Case $1$: Two knights are placed on the board such that one is at the corner and the other is in the center, as shown below. For the other two remained knights, squares $3,7,8,16$ cannot be occupied. In this manner, the two other knights could be placed, as a pair, at: $$\{ (4,6), (5,11), (6,12), (9,15),(11,13),(12,14),\\ (2,9),(5,14),(6,13), (2,11),(6,15), (4,11)\}.$$

enter image description here

In this case, there are 8 ways of placing the first two knights. So, the total number of possible positions in this case is $8 \times 12=96.$


Case $2$: Two knights are placed on the board such that both are at the edge, as shown below. For the other two remained knights, squares $7,8,11,15$ cannot be occupied. In this manner, the two other knights could be placed, as a pair, at: $$\{(3,5),(4,6),(6,12),(10,16),(12,14),(1,10),(6,13),(5,14),(3,10),(3,12)\}.$$ enter image description here

In this case, there are 8 ways of placing the first two knights. So, the total number of possible positions in this case is $8 \times 10=80.$


Case $3$: Two knights are placed on the board such that one is at the edge and the other is in the center, as shown below. For the other two remained knights, squares $4,8,5,9,13$ cannot be occupied. In this manner, the two other knights could be placed, as a pair, at:

$$\{(1,7),(6,12),(10,16),(12,14),(1,10),(3,10),(6,15),(7,14),(3,12),(7,16)\}.$$

enter image description here

In this case, there are 8 ways of placing the first two knights. So, the total number of possible positions in this case is $8 \times 10=80.$


Finally, note that every possible position belongs to one of the cases above, and every possible position is counted twice. Hence, the final answer is:

$$\frac{80+80+96}{2}=128.$$