# 回溯算法

当问题是要求满足某种性质(约束条件)的所有解或最优解时,往往需要使用回溯法

回溯的本质就是利用 DFS 穷举一棵决策树,解题时有 3 个要点需要把握:

  1. 记录已经走过的路径

  2. 维护选择列表

  3. 明确终止条件

backtrack(choices,curPath,res):
    if 满足条件 :
        res.add(curPath);
        return;
    for choice in choices:
        curPath.select(choice)
        backtrack(newChoices, curPath, res)
        curPath.undoSelect()
1
2
3
4
5
6
7
8
Last Updated: 9/11/2020, 6:30:35 AM