def draw_final_path(state):
'''Draw the TSP path given the cities in the correct order'''
if not state:
return None
states = []
while state:
states.append(state)
state = state.parent
cities = [state.current_city for state in states]
rest = deepcopy(cities)
rest = rest[1:]
rest.reverse()
cities = [cities[0]] + rest
turtle.clear()
turtle.hideturtle()
turtle.up()
turtle.pensize(1)
for city in cities:
x = city.position[0]
y = city.position[1]
turtle.pencolor("red")
turtle.goto(x, y)
turtle.pencolor("black")
if city.is_start:
turtle.write('{}-Start'.format(city.name), align="center", font=("Arial", 11, "bold"))
else:
turtle.write('{}'.format(city.name), align="center", font=("Arial", 11, "bold"))
turtle.down()
turtle.pencolor("red")
turtle.goto(cities[0].position[0], cities[0].position[1])
评论列表
文章目录