In how many ways can I arrange
black and
white pawns on an
chessboard such that no pawn attack another?
For reference:
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The main idea behind my code is the following observation:
Color the squares white and blue and consider them separately. Let's look at the blue.
Lemma: if you place a white pawn on a blue square, then all the blue squares above or at the two blue diagonals from the white pawn are also white.
Proof: if one of the blue squares above had a black pawn, then it would attack the white pawns adjacent to it.
With this observation, we see that to count the number of ways to cover the blue squares white, we just have to draw a jagged line dividing the board into two pieces:
(the 0 and 1 rows are there if there are no white pawns in that respective column) Note that we can represent this jagged line with an integer sequence of length 8, with consecutive elements have difference exactly one. In the above scenario, this sequence is 4 5 4 5 6 5 4 5 Now we want to count the number of white pawns per sequence, so we need to enumerate all sequences. This can be done by first picking a start point of either 0 , 2 , 4 , 6 , 8 then choosing either to go diagonal up or diagonal down by looping through all binary numbers 0 0 0 0 0 0 0 → 1 1 1 1 1 1 1 and getting rid of ones that go over the edge.
Finally, after we find how many pawns per sequence, we plug them in an array holding the generating function for the number of ways to place n white pawns, then multiply this with itself (to account for white and blue squares) and finding the coefficient of x 3 2 , corresponding to 3 2 white pawns total.
My Java code (terminates in 14 ms):