编辑代码

#include <iostream>
using namespace std;
struct ResultNode;
struct Item {
    string name;
    int weight;
    int value;

    Item(string n, int w, int v) {
        name = n;
        weight = w;
        value = v;
    }

    Item() {
        name = "0";
        weight = 0;
    }
};

struct ResultNode
{
    Item *items;//选中的物品数组
    int itemsCount;//选中物品的数量
    ResultNode *next;//下一个可能的选择结果

    ResultNode(Item *itemsArray, int itemsArrayCount, ResultNode *nextNode) {
        items = itemsArray;
        itemsCount = itemsArrayCount;
        next = nextNode;
    }
};

void deleteResultList(ResultNode *head) {
    if (NULL == head) {
        return;
    }

    ResultNode *curNode = head->next;
    while (curNode != NULL)
    {
        ResultNode *nodeToDelete = curNode;
        curNode = curNode->next;
        delete nodeToDelete;
    }
    
}

ResultNode* createResultNode(Item *items, int itemCount, int *itemSelectResult){

    Item *itemsSelected = new Item[itemCount]();
    int count  = 0;

    for (size_t i = 0; i < itemCount; i++)
    {
        if (itemSelectResult[i] == 1) {
            itemsSelected[count].name = items[i].name;
            itemsSelected[count].weight = items[i].weight;
            itemsSelected[count].value = items[i].value;
            ++count;
        }
    }

    return new ResultNode(itemsSelected, count, NULL);
    
}
void backpack(int packWeight, 
              int &maxValue, 
              Item *items, 
              int itemCount, 
              int curItemCount, 
              int curWeight, 
              int curValue, 
              ResultNode *head, 
              int *itemSelectResult) {
    
    // 终止条件
    // 所有物品都决策完了,选与不选,到了满足条件的(能够装包)叶子结点
    if (itemCount == curItemCount) {
        // 物品选择完了(到了解空间的叶节点)
        if (curValue > maxValue) {
            maxValue = curValue;
            // 删除之前的结果
            deleteResultList(head);
            // 重新记当前的结果
            head->next = createResultNode(items, itemCount, itemSelectResult);
        }
        else if (curValue == maxValue) {
            // 最大值相同的结果给记录下来
            ResultNode* newNode = createResultNode(items, itemCount, itemSelectResult);
            newNode->next = head->next;
            head->next = newNode;
        }
        return;
    }

    // 大问题转换成小问题
    Item *curItem = &items[curItemCount];//当前子树的根节点

    // 遍历左子树
    // 不装它
    itemSelectResult[curItemCount] = 0;
    //要装包的物品数量减少了1个,大问题转换成了小问题
    backpack(packWeight, maxValue, items, itemCount, curItemCount+1, curWeight, curValue, head, itemSelectResult);

    // 遍历右子树
    // 装进去
    if (curWeight + curItem->weight <= packWeight){
        // 满足限制条件
        // 一个解[1,1,1]
        itemSelectResult[curItemCount] = 1;
        
        //要装包的物品数量减少了1个,大问题转换成了小问题
        backpack(packWeight, maxValue, items, itemCount, curItemCount + 1, curWeight + curItem->weight, curValue + curItem->value, head, itemSelectResult);
    }

    

}

void printList(ResultNode *head)
 {

    if (NULL == head) {
        return;
    }
    ResultNode *curNode = head->next;
    while ( curNode != NULL)
    {
        cout << "-------------" << endl;
        for (size_t i = 0; i < curNode->itemsCount; i++)
        {
            cout << curNode->items[i].name << ":" << curNode->items[i].weight << ":" << curNode->items[i].value << endl;
        }
        curNode = curNode->next;
    
    }   
}

int main() {

     int packWeight = 10;
     Item items[] = {
         Item("物品1", 6, 5),
        Item("物品2", 5, 3),
         Item("物品3", 4, 5),
         Item("物品4", 2, 3),
         Item("物品5", 1, 2),
     };
     int itemCount = sizeof(items)/sizeof(Item);
     int *itemSelect = new int[itemCount]();
     ResultNode head(NULL, 0, NULL);
     int maxValue = 0;

    backpack(packWeight, maxValue, items, itemCount, 0, 0, 0, &head, itemSelect);

     cout << "Max value is " << maxValue << endl;
     printList(&head);
     
}