Classroom Programming
page was last updated: Sep 12, 2018 19:24
forum
Knight's Tour P3

Requirements

Objective

  1. Start with a 8x8 grid
  2. Ask where knight starts
  3. Loop and find all possible routes knight can use to land on every space only once.
  4. Display routes
  5. Bonus
    1. variable size of board
    2. option to end where started

This is a classic data structures class programming excercise. This is not that easy. There are many different ways to solve this puzzle. A chess knight moves in L shaped patterns (look it up on wiki). The Knight's Tour excercise is where the knight, starting from somewhere on the board, moves to every space on the board but landing in each space only once.

Many suggest using function recursion, but you'll quickly learn that there is a limit to how many levels deep you can go. This is fine for a typical board but what if I tested on a larger board? What if I don't have a cool OS like linux to let me go deeper? (Windows recursion limit is MUCH lower than in linux last time I tested. ~30,000 vs ~150,000) You can of course do it however you want. My first try uses it and exploits function stack arrays in a stupid way. (but it was short (: )

Example

Knight's Tour Start column (a-h): a Start row (1-8): 1 Possible Routes: A1,B3,...... lots of coordinates |