Algorithm 13.4 BacktrackRec Inputs 集合X1, X2, ..., Xn的清楚或隐含的描述 Outputs 解向量 u = (x1, x2, .., xi), 0<=i<=n v = () flag = False advance(1) if flag: output v # v是解向量 else: output "no solution"
def advance(k) for x ∈ Xk xk = x; xk加入v if v为最终解: flag=True and exit elif v 是部分的:advance(k+1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
迭代的形式采用了两个while循环:外层循环回溯,内层循环前进 Alg 13.5 BacktrackIter v = () flag = False k = 1 while k>=1 while Xk 还未被完全穷举 xk = next(Xk); xk加入v if v为最终解: flag=True and exit elif v是部分的: k = k + 1 # 前进 {Forward} 重置 Xk,使得下一个元素排在第一位 k = k - 1 # 回溯 {Backward}
if flag: output v # v是解向量 else: output "no solution"
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.