5. 迭代回溯
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 迭代回溯
template <class Type>
Type MaxLoading(Type w[],Type c,int n,int bestx[]){
// 返回最优装载量及其相应解,初始化根节点
int i=1, // 当前层
j;
int *x=new int[n+1]; // x[1:i-1]为当前路径
Type bestw=0, // 当前最优载重量
cw=0, // 当前载重量
r=0; // 剩余集装箱重量
for(j=1;j<=n;j++)
r+=w[j];
while(true){ // 搜索子树
while(i<=n && cw+w[i]<=c){ // 进入左子树
r -= w[i];
cw +=w[i];
x[i]=1;
i++;
}
if(i>n){ // 到达叶节点
for(j=1;j<=n;j++)
bestx[j]=x[j];
bestw=cw;