Working On My Knight Moves

Let f ( x , y ) f(x, y) return the number of places a knight can move to (in one move) on a standard 8 × 8 8\times8 chess board from the given ( x , y ) (x, y) position. What is the sum of f ( x , y ) f(x, y) for all x x and y y ?

Here are some explicit examples:

  • f ( 0 , 0 ) = 2 f(0, 0) = 2 , f ( 5 , 3 ) = 8 f(5, 3) = 8 , f ( 7 , 7 ) = 2 f(7, 7) = 2 .

Inspired by Bob Seger's "Night Moves" .


The answer is 336.

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.

4 solutions

Calvin Lin Staff
Mar 28, 2016

There are 8 types of knight moves, corresponding to ( ± 1 , ± 2 ) , ( ± 2 , ± 1 ) (\pm 1, \pm 2 ) , ( \pm 2, \pm1 ) .

Let's consider how many valid moves there are for the case of ( + 1 , + 2 ) (+1, +2) .
We are moving 1 to the right and 2 to the top. Hence, we cannot start in the right most column, nor can we start in the top 2 rows. Given any other starting position, it is clear we can move ( + 1 , + 2 ) (+1, +2) . Hence, there are 7 columns × \times 6 rows of possible starting positions, which gives us 42 total.

This analysis is the same for each of the 8 moves, and each of them has 42 starting positions. Hence, there are a total of 8 × 42 = 336 8 \times 42 = 336 possible moves that the knight can make.


Note: This analysis generalizes easily to a n × m n \times m board. What is the generalized answer?

Arjen Vreugdenhil
Mar 28, 2016

Generally, on a n × n n \times n board one can make a translation ( x , y ) (x, y) from ( n x ) ( n y ) (n-|x|)\cdot (n-|y|) different positions.

The knight has eight translations as possible moves, with x = 1 |x| = 1 and y = 2 |y| = 2 or vice versa.

Thus there are 8 ( n 1 ) ( n 2 ) 8\cdot (n-1)\cdot (n-2) possible knight moves on an n × n n \times n board, and with n = 8 n = 8 we get 8 7 6 = 336 8\cdot 7\cdot 6 = 336 .

Great explanation that generalizes this problem in a different way :)

Calvin Lin Staff - 5 years, 2 months ago
Vishnu Bhagyanath
Mar 23, 2016

Note that the 8 × 8 8\times 8 grid is symmetric about the center and hence, f ( x , y ) = f ( 8 x , y ) = f ( x , 8 y ) = f ( 8 x , 8 y ) f(x,y) = f(8-x,y) =f(x,8-y)=f(8-x,8-y)

Consider a 4 × 4 4\times 4 grid as shown below

Notice that a knight on the corner ( marked red ) can move to only two squares. A knight on a yellow square can move to 3 squares. A knight on the green squares can move to 4 squares. On the blue, 6 squares, and on the purple, no restriction (all 8 knight moves are possible).

Hence the total will be 4 × ( 4 × 8 + 4 × 6 + 5 × 4 + 2 × 3 + 1 × 2 ) = 336 4 \times ( 4 \times 8 + 4\times 6 + 5\times 4 + 2 \times 3 + 1 \times 2 ) = 336

Nice solution. Solving f ( x , y ) f(x, y) for only 16 values and using that information to figure out the rest is a great way to go about it. Using your solution, it would only take another short step to generalize this to an n × n board.

Brock Brown - 5 years, 2 months ago

Log in to reply

There is a one line solution to this problem.

Hint: 8 × 42 8 \times 42 .

Calvin Lin Staff - 5 years, 2 months ago
Brock Brown
Mar 23, 2016

Python 3:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
count = 0
def f(x, y):
    moves = 0
    for dx in (-2,-1,1,2):
        for dy in (-2,-1,1,2):
            if abs(dx) != abs(dy):
                x1, y1 = dx+x, dy+y
                if x1 <= 7 and x1 >= 0 and y1 <= 7 and y1 >= 0:
                    moves += 1
    return moves
for x in range(8):
    for y in range(8):
        count += f(x, y)
print ("Answer:", count)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...