为什么我写的程序老是崩溃,跪求大神帮忙看一下(回朔法求0/1背包问题)
#include<iostream>using namespace std;
int n;//物品个数
int BackpackVolume;//背包容量
int maxValue;//最大价值
int *weight = new int;//物品的重量
int *value = new int;//物品的价值
int *bestPath = new int;//最佳路径
int *judge = new int; //判断左右分支
int currentWeight=0;//当前重量
int currentValue=0;//当前价值
void dfs(int i) {
if(i>n){
if (currentValue > maxValue) {
maxValue = currentValue;
for(i=1;i<=n;i++){
bestPath = judge;
}
}
}
else
{
for (int j = 1; j >= 0; j--) {
judge = j;
if (currentWeight + weight* judge <= BackpackVolume) {
currentWeight += weight * judge;
currentValue += value * judge;
dfs(i + 1);
currentWeight-= weight * judge;
currentValue -= value* judge;
}
}
}
}
int main()
{
int i;
maxValue = 0;
cout << "请输入背包最大容量:" << endl;;
cin >> BackpackVolume;
cout << "请输入物品个数:" << endl;
cin >> n;
cout << "请依次输入物品的重量:" << endl;
for (i = 1; i <= n; i++)
cin >> weight;
cout << "请依次输入物品的价值:" << endl;
for (i = 1; i <= n; i++)
cin >> value;
dfs(1);
cout << "最大价值为:" << endl;
cout << maxValue << endl;
cout << "被选中的物品的标号依次是:" << endl;
for (i = 1; i <= n; i++){
if (bestPath == 1)
cout << i << " ";
}
cout << endl;
return 0;
}
第55行一运行就崩溃???????????????????????
页:
[1]