-3

Given an N x N chess board and a White King that can move one step in only two directions: right and up. (Assume that the King is at the bottom left square in the starting position). Moreover, once the King starts moving, it can change direction only once. E.g. if it starts with first step towards right, it can either keep going right or turn moving upwards at some point. Thereafter, it is not allowed to move right again. Given ’n’ steps/moves for the King, how may maximum distinct squares can it reach or pass through on the chess board? Design a suitable algorithm to solve this problem and calculate its running time.

An Nam
  • 1

1 Answers1

2

Answer: (n * (n + 1))/ 2 + n - 1 squares.

You just need to observe the possibilities with small n values to verify.

Let's see for some small ns:

n = 3     n = 4       n = 5         n = 6      ...

x x o    x x o o    x x o o o    x x o o o o
x x x    x x x o    x x x o o    x x x o o o
s x x    x x x x    x x x x o    x x x x o o
         s x x x    x x x x x    x x x x x o
                    s x x x x    x x x x x x
                                 s x x x x x

s: where king starts
x: squares he can reach
o: squares he cannot reach

It's easy to see that he reaches all the bottom left half of the matrix plus a diagonal number of n - 1 squares:

n = 6

x\ x \ o o o o
xx\ x \ o o o
xxx\ x \ o o
xxxx\ x \ o
xxxxx\ x \
sxxxxx\   \

(n*(n+1))/2 + (n-1)
Daniel
  • 7,357
  • 7
  • 32
  • 84