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.