一、 数据结构
int x[N]; //解向量
二、回溯算法搜索子集树
void traceback(int t){
if(t > n) update(x); // 更新解向量
else
for(int i=0; i<=1; i++){
x[t] = i; // 每个结点只有两个子树, 0或1
// constraint是约束函数, bound是限界函数(可行性剪枝)
if(constraint(t) && bound(t)) traceback(t+1);
}
}
三、回溯算法搜索排列树
void traceback(int t){
if(t > n) update(x);
else
for(int i=t; i<=n; i++){
swap(x[t], x[i]); // 全排列
traceback(t+1);
swap(x[t], x[i]); // 恢复状态
}
}