递归回溯:
private static void backtrack(int t){
if t>=n then //递归深度t大于等于最大层次n,输出解向量x
output(x)
for j<-f(n,t) to g(n,t) do //f(n,t)为子树的起始编号,g(n,t)为子树的终止编号
x[t]<-value(i) //将当前拓展节点x[t]赋上所选子树的值
if constraint(t)&&bound(t) then //constraint(t)约束函数、bound(t)界函数
backtrack(t+1)
}
迭代回溯:
void iterativeBacktrack ()
{
t=1;
while t>0 then
if f(n,t)<=g(n,t) then //当前是否还有节点可选
for i<-f(n,t) to g(n,t) do //遍历所有节点
x[t]<-value(i)
if constraint(t)&&bound(t) then
if solution(t) then
output(x)
else
t++
else
t--
}