1

I'm trying to implement Knight's Tour using DFS in Racket.

My current implementation involves generating a list of legal moves (not off board and not in list of previous steps) possible from the current position and throwing it into a stack. Then I'm taking the first element off the stack and recursively running the algorithm with that being the next current position. If the number of moves got over the number of squares on the board, then I knew I had made an exhaustive search and failed. If I could not generate any legal nodes, I knew I had completed the knights tour and output the resulting multi-dimensional array. I also counted the number of children (legal moves) generated; if that number gets greater than a pre-set max_node_count it would abort the algorithm.

I'm thinking that this implementation might be missing something. If I just move to the next position in the stack, won't I be running around the board without necessarily making a knight's tour? How can I keep track of the previous correct position and backtrack? Or should I use a different condition to check for completion. I think I'm cutting the algorithm off early by accident.

When I run my current implementation, I get the message indicating that I've exhausted the search. I'm testing on a 5x5 board so there should definitely be a solution.

I'm thinking that there is a problem with either my check condition for when I've made an exhaustive search, or when I've completed a knight's tour. My comment below should give some more detail on how I implemented these checks.

Thanks for reading, and thanks for the help.

false
  • 10,264
  • 13
  • 101
  • 209
user1846359
  • 233
  • 3
  • 12
  • Did you record all the previous moves? if this is DFS, you should avoid to repeat same move! – Pham Trung Feb 05 '14 at 07:16
  • Yeah, previous moves are recorded and a successor will not be generated to represent a move if its already in the list of previous moves. – user1846359 Feb 05 '14 at 08:00
  • So this should be enough, this step already guarantees that you will not be trapped into any infinite loop. – Pham Trung Feb 05 '14 at 08:56
  • But when I run the code, it tells me its done an exhaustive search, and failed to find a path on a 5x5 board. On a board that size, there should definitely be a solution. I think my condition for when I've performed an exhaustive search is wrong, or my win condition is wrong. My win condition is if I cannot generate any successors. My exhaustive search condition is if the number of steps taken so far is greater than the number of squares available on the board. – user1846359 Feb 05 '14 at 19:02
  • I'm starting to think that when I cannot generate any more successors I've made an exhaustive search. If thats the case, then I need a new win condition. Maybe I can scan the board and if all elements are greater than 0 (array is initialized with all 0s) then I've succeeded. Each visited square is marked with a step number indicating the order each square was visited. – user1846359 Feb 05 '14 at 19:20
  • @KarolyHorvath Oh wow, thanks for the tip. LOL. But I'm running the same algorithm on a 16x16 board for which there should be a solution (I googled it this time) and it still tells me its exhausted. Probably still something wrong with my exhausted or win condition? – user1846359 Feb 05 '14 at 19:30
  • @KarolyHorvath According to Wikipedia there are 1728 solutions for a 5x5 board - see http://en.wikipedia.org/wiki/Knight's_tour – uselpa Aug 09 '14 at 22:14
  • @uselpa: yes, for the open tour. and no solutions for the closed tour. – Karoly Horvath Aug 09 '14 at 22:17
  • @KarolyHorvath Did the OP specifically ask for the closed tour? I cannot find anything specifying open or closed in the question. – uselpa Aug 09 '14 at 22:23
  • oh well, see my first comment :DDD – Karoly Horvath Aug 09 '14 at 22:24

0 Answers0