如何算出所有骑士没有走过的点并且算出距离近的目标先走?
Warnsdorff’s rule is a simple rule for a single knight’s tour that works most of the time on smaller boards. For a knight at some location (x,y) on a board:
- find all the unvisited locations Li that the knight can reach in one move
- if there is no such location Li then the tour ends (either in failure, or the knight has visited every square of the board)
- for each location Li found in Step 1:
- count the number Ni of unvisited locations that the knight can reach in one move from Li
- the next move in the tour is the location Li having the smallest non-zero count Ni
- if two or more locations Li have the same minimum count then choose any one of those locations as the next move
1.初始化一个棋盘,并标记起点为已经访问。
2.对于当前位置,计算所有未访问过的位置可以在一个移动内到达的数量和位置。可以使用 Warnsdorff 的规则,为每个位置计算它们可以到达的未访问的位置数量。
3.选择可以到达未访问位置数量最少的位置作为下一个要移动的位置。如果有多个位置可以到达相同的数量,则选择其中一个位置作为下一个要移动的位置。
4.标记选择的下一个位置为已访问,并将当前位置更新为选择的下一个位置。
重复步骤 2-4 直到所有位置都被访问。
使用这种算法可以保证骑士遍历整个棋盘,同时最小化每个步骤的距离。