Skip to content
Advertisement

Knight’s tour backtracking

I am working on knight’s tour problem, and using backtracking algorithm. My code doesn’t produce the right output in the end it just repeats the last two entries over and over until n^2 -1 is not reached.

This is my code. I am following this pseudocode http://www.wou.edu/~broegb/Cs345/KnightTour.pdf

JavaScript

Advertisement

Answer

You are settings visited[x][y]=True to true at the end of your algorithm. It has to be set after you check you’ve bin there. I also made a couple of enhancements for your code:

JavaScript
Advertisement