回溯中是否有 do -> recurse -> undo 模式的名称?

Is there a name for the do -> recurse -> undo pattern in backtracking?

backtracking中,即解决n皇后问题的算法,基本上有两种递归调用的方法:

  1. 复制父板做子板,修改子板放置新皇后,然后递归调用子板
  2. 直接修改板子,递归调用,然后撤消修改。

首选第二种,因为它避免了昂贵的复制。

这种选择也存在于其他算法中,例如游戏中的 minimax。

模式 2 是否有与模式 1 相对的名称?

与此相关的主题是面向对象编程中的设计模式,称为"command-pattern"。它有助于根据命令堆栈撤消最近的命令。

在约束规划和 SAT 求解(您的 n 皇后示例通常来自哪里)中,我认为这些概念被描述为:

  • 基于副本
  • trail(ing)-based

参见示例:

Reischuk, Raphael M., et al. "Maintaining state in propagation solvers." International Conference on Principles and Practice of Constraint Programming. Springer, Berlin, Heidelberg, 2009.

Schulte, Christian. "Comparing Trailing and Copying for Constraint Programming." ICLP. Vol. 99. 1999.

前者节选:

Constraint propagation solvers interleave propagation, removing impossible values from variable domains, with search. The solver state is modified during propagation. But search requires the solver to return to a previous state. Hence a propagation solver must determine how to maintain state during propagation and forward and backward search. [...] This paper also provides the first realistic comparison of trailing versus copying for state restoration.

两者各有优缺点,详见参考文献。

请记住,踪迹 通常不仅用于存储您的决定(板放置),而且还发生传播(由于以下原因,此放置导致这些现在不可能的放置alldifferent 传播:这些影响也必须恢复!)。有关此类实施的概述,请参阅:MiniCP

我会说这两个是相同的算法,只是有一个 mutable 或者 immutable board.

我还要说的是,对于 n-queens 的特定情况,副本根本不贵,因为您可以仅使用 64 位表示一块板.. . 你可能比调用堆栈的每个级别花费更多 :)