Algorithm 理解回溯算法的伪码

Algorithm 理解回溯算法的伪码,algorithm,backtracking,Algorithm,Backtracking,要启动回溯跟踪算法,可以为i=0调用以下伪码;X[1..0]表示空元组 ALGORITHM Backtrack(X[1..i]) //Gives a template of a generic backtracking algorithm //Input: X[1..i] specifies first i promising components of a solution. //Output: Alll the tuples representing the problem

要启动回溯跟踪算法,可以为i=0调用以下伪码;X[1..0]表示空元组

ALGORITHM Backtrack(X[1..i])
   //Gives a template of a generic backtracking algorithm
   //Input: X[1..i] specifies first i promising components of a solution.
   //Output: Alll the tuples representing the problem's solutions
   If X[1..i] is a solution write X[1..i]
   else
     for each element x belongs to Si+1 consistent with X[1..i] and constraints do
        X[i+1] <- x
        Backtrack(X[1..i+1])
算法回溯(X[1..i])
//给出了一个通用回溯算法的模板
//输入:X[1..i]指定解决方案的第一个组件。
//输出:所有表示问题解决方案的元组
如果X[1..i]是解决方案,则写入X[1..i]
其他的
对于每个元素,x属于与x[1..i]一致的Si+1,且约束条件为

X[i+1]看看这个PDF文件,第25页。它有一个步骤到步骤的描述与图像约4皇后解决方案与回溯

简言之:

该算法试图为数组X的每个元素找到一个与所有约束一致的赋值

要通过回溯实现这一点,我们使用递归函数。在每个步骤中,我们检查当前变量(域集Si+1)的所有可用值,如果它与约束一致,我们将递归地转到下一个变量,直到找到解决方案