1. from pythonds.graphs import Graph, Vertex
2. def knightTour(n, path, u, limit):
3. u.setColor('gray')
4. path.append(u)
5. if n < limit:
6. nbrList = list(u.getConnections())
7. i = 0
8. done = False
9. while i < len(nbrList) and not done:
10. if nbrList[i].getColor() == 'white':
11. done = knightTour(n+1,
12. path,
13. nbrList[i],
14. limit)
15. i = i + 1
16. if not done: # 准备回溯
17. path.pop()
18. u.setColor('white')
19. else:
20. done = True
21. return done
```python
```python
```python
在这里插入代码片