0

Normally knights move (1,2) steps at a time i.e 1 step in one direction, and two in the other. In a general version it can move (i,j) steps at a time.

I'm not sure if this is a knight's tour problem, since I don't remember a restriction on visiting one square at a time. Also, the answer is just a yes/no, we don't need to know the actual path.

One idea I had is essentially treating the board points like a graph and doing a depth first search for all valid (i,j) moves and marking it as visited. At the end of it, if there's any unvisited square, then it's not possible. However, this takes up N^2 space and I wanted to know if there was a simpler solution since it's a yes/no question.

false
  • 10,264
  • 13
  • 101
  • 209
ssh
  • 195
  • 1
  • 11
  • N² is peanuts compared to the probably exponential computing time, which might just make a brute-force approach infeasible. –  Oct 13 '16 at 12:02

1 Answers1

0

Its rather simple.

If there is NxN board and knight moves a,b, then:

  1. If gcd(a,b)==1 and
  2. If center can be reached**

Knight can cover the whole board.

**To determine if center can be reached:

"Let center square be (x,y). Then, one of these point-pairs must be in-range (1..N, 1..N)"

(x +/- a, y +/- b) and (x +/- b, y +/- a)

which are essentially 8 points where knight can move if it was at center of the board.

Community
  • 1
  • 1
vish4071
  • 5,135
  • 4
  • 35
  • 65