freeCodeCamp/guide/chinese/algorithms/backtracking-algorithms/index.md

2.1 KiB
Raw Blame History

title localeTitle
Backtracking Algorithms 回溯算法

回溯算法

回溯是一种通用算法用于查找某些计算问题的所有或某些解决方案特别是约束满足问题逐步构建候选解决方案并在确定候选不能时立即放弃每个部分候选_“回溯”_可能完成一个有效的解决方案。

示例问题(骑士游览问题)

骑士被放置在空板的第一块上,按照国际象棋的规则移动,必须完全访问每个方块一次。

###路径跟随Knight覆盖所有细胞 以下是8 x 8单元的棋盘。单元格中的数字表示骑士的移动数量。 骑士的旅行解决方案 - 由欧拉

骑士之旅的天真算法

朴素算法是逐个生成所有游览并检查生成的游览是否满足约束。

while there are untried tours 
 { 
   generate the next tour 
   if this tour covers all squares 
   { 
      print this path; 
   } 
 } 

骑士巡回赛的回溯算法

以下是奈特巡回赛问题的回溯算法。

If all squares are visited 
    print the solution 
 Else 
   a) Add one of the next moves to solution vector and recursively 
   check if this move leads to a solution. (A Knight can make maximum 
   eight moves. We choose one of the 8 moves in this step). 
   b) If the move chosen in the above step doesn't lead to a solution 
   then remove this move from the solution vector and try other 
   alternative moves. 
   c) If none of the alternatives work then return false (Returning false 
   will remove the previously added item in recursion and if false is 
   returned by the initial call of recursion then "no solution exists" ) 

更多信息

维基百科

极客4极客

一个非常有趣的回溯介绍