Python到C++:使用递归列出背包的所有组合的算法
Python to C++: Algorithm that list all combinations of Knapsack using recursion
我正在尝试实现一个代码,该代码使用递归列出背包问题的所有可能组合。我在递归方面有困难。我试图解决它,但一无所获,所以我做了一些研究,在JavaPython中找到了一个代码,但我很难用C++重写那个代码。
以下是JavaPython:中的解决方案代码
items = [1,1,3,4,5]
knapsack = []
limit = 7
def print_solutions(current_item, knapsack, current_sum):
#if all items have been processed print the solution and return:
if current_item == len(items):
print knapsack
return
#don't take the current item and go check others
print_solutions(current_item + 1, list(knapsack), current_sum)
#take the current item if the value doesn't exceed the limit
if (current_sum + items[current_item] <= limit):
knapsack.append(items[current_item])
current_sum += items[current_item]
#current item taken go check others
print_solutions(current_item + 1, knapsack, current_sum )
print_solutions(0,knapsack,0)
我在这个链接中发现了那个代码
这是我尝试做的。
#include <iostream>
using namespace std;
void AddItem(int item, int *knapsack) {
int i = 0;
while (knapsack[i] != -1)
i++;
knapsack[i] = item;
};
void printKnapsack(int *knapsack, int n) {
cout << "[";
for (int i = 0; i < n; i++)
if (knapsack[i] != -1)
cout << knapsack[i] << ",";
}
void print_solutions(int current_item, int *knapsack, int current_sum, int *items, int n, int limit) {
//if all items have been processed print the solution and return
if (current_item == n - 1) {
printKnapsack(knapsack, n);
return;
};
//don't take the current item and go check others
print_solutions(current_item + 1, knapsack, current_sum, items, n, limit);
//take the current item if the value doesn't exceed the limit
if (current_sum + items[current_item] <= limit) {
AddItem(items[current_item], knapsack);
current_sum += items[current_item];
};
//current item taken go check others
print_solutions(current_item + 1, knapsack, current_sum, items, n, limit);
};
int main() {
int current_item = 0;
int current_sum = 0;
int limit, n;
cout << "Type the maximum weight ";
cin >> limit;
cout << "How many items? ";
cin >> n;
int* knapsack;
knapsack = new int[10];
for (int i = 0; i < 10; i++)
knapsack[i] = -1;
int * items;
items = new int[n];
cout << "Type weights.";
for (int i = 0; i < n; i++) {
cin >> items[i];
};
print_solutions(0, knapsack, 0, items, n, limit);
return 0;
}
带输入:
7 // limit
5 // number of items
1 1 3 4 5 // items
我希望得到以下最终结果:
[]
[5]
[4]
[3]
[3, 4]
[1]
[1, 5]
[1, 4]
[1, 3]
[1]
[1, 5]
[1, 4]
[1, 3]
[1, 1]
[1, 1, 5]
[1, 1, 4]
[1, 1, 3]
但我得到的只是用3和4填充的数组,而不是所有实际的解决方案。
简而言之
在将算法从python转换为C++的过程中,有一个主要问题与参数传递相关的语言语义有关。
详细信息
在python中,您可以编写以下内容:
print_solutions(current_item + 1, list(knapsack), current_sum)
则list(knapsack)
是来自knapsack
列表的副本。因此,中间的递归调用保持原始knapsack
不变,而第二个递归调用更改原始knapsack
:
print_solutions(current_item + 1, knapsack, current_sum)
然而,在C++代码中,在这两种情况下,您都处理原始的knapsack
列表(数组参数通过引用传递),因此knapsack
会完全混乱:
//don't take the current item and go check others
print_solutions(current_item + 1, knapsack, current_sum, items, n, limit);
如何使其发挥作用
要么创建一个临时数组并在其中复制knapsack
,要么更好的是,开始使用vector
,这将使您的C++生活更加轻松(注意通过值或引用传递)。
以下版本使用矢量。参数中的&
表示它是通过引用传递的参数(即原始向量可以更改)。请注意,我们不再需要传递n
,因为向量知道它的长度,就像python中的list所做的那样:
void print_solutions(int current_item, vector<int>& knapsack, int current_sum, const vector<int>& items, int limit) {
//if all items have been processed print the solution and return
if (current_item == items.size() ) {
printKnapsack(knapsack);
return;
};
//don't take the current item and go check others
vector<int> knapcopy = knapsack;
print_solutions(current_item + 1, knapcopy, current_sum, items, limit);
//take the current item if the value doesn't exceed the limit
if (current_sum + items[current_item] <= limit) {
knapsack.push_back(items[current_item]);
current_sum += items[current_item];
//current item taken go check others
print_solutions(current_item + 1, knapsack, current_sum, items, limit);
};
};
这是一个在线演示。
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 基于ELO的团队匹配算法
- C++选择排序算法中的逻辑错误
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- C++A*算法并不总是在路径中具有目标节点
- 排序算法c++
- 0-1背包代码中的错误.我的代码中有什么错误
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 算法问题:查找从堆栈中弹出的所有序列
- 下面是排序算法O(n)吗
- KMP算法和LPS表构造的运行时间
- 为什么我的排序算法会更改数组值
- 求最大元素位置的分治算法
- 具有非整数边容量的最大流量的Dinic算法
- 到连接组件算法的问题(递归)
- Python到C++:使用递归列出背包的所有组合的算法
- 有没有解决方案而不是在C++中移动背包回溯算法
- 两个袋子的背包算法
- 为背包算法使用向量时抛出了错误的alloc
- 如何使用背包算法找到袋子中的哪些元素[而不仅仅是袋子的价值]?