Skip to content

Instantly share code, notes, and snippets.

@mikezink
Created October 22, 2019 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikezink/501c90f937c0565839ade25d2b8fdc47 to your computer and use it in GitHub Desktop.
Save mikezink/501c90f937c0565839ade25d2b8fdc47 to your computer and use it in GitHub Desktop.
from pythonds.graphs import Graph
from knightGraph import knightGraph
def knightTour(n,path,u,limit):
print("Current Vertex:{}".format(u))
u.setColor('gray')
path.append(u)
if n < limit:
nbrList = list(u.getConnections())
i = 0
done = False
while i < len(nbrList) and not done:
if nbrList[i].getColor() == 'white':
done = knightTour(n+1, path, nbrList[i], limit)
i = i + 1
if not done: # prepare to backtrack
bt_vertex = path.pop()
u.setColor('white')
print("Backtracking vertex {}".format(bt_vertex))
else:
done = True
print("Final path")
for vertex in path:
print(vertex)
return done
path = []
kG = knightGraph(5)
knight_start_position = 15
v=kG.getVertex(knight_start_position)
number_of_squares_to_visit = 18
knightTour(0, path, v, number_of_squares_to_visit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment