#include<iostream>
#include<iomanip>
using namespace std;
int** Get2Array(int row, int col)
{
int** s = (int **)malloc(sizeof(int*)*row);
for (int i = 0; i < col; i++)
{
s[i] = (int*)malloc(sizeof(int)*col);
memset(s[i], 0, sizeof(int)*col);
}
return s;
}
void Print2Array(int** s, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << setw(4) << s[i][j];
}
cout << endl;
}
cout << endl;
}
int KnapSack(int n, int m,int i, int j, int* w,int** c)//i区域(i到n),j背包的价值,w[]商品的价值
{
if (c[m][n] > 0)
{
return c[m][n];
}
if (i == n)
{
c[m][n] = KnapSack(n, m, i + 1, j - w[i], w, c);
}
else
{
if (j < w[j])
{
c[m][n] = KnapSack(n, m, i + 1, j , w, c);
}
else
{
int max1 = KnapSack(n, m, i + 1, j-w[i], w, c);
int max2 = KnapSack(n, m, i + 1, j, w, c);
if (max2 > max1)
{
c[m][n] = max2;
}
else
{
c[m][n] = max1;
}
}
}
return c[m][n];
}
建立一个二维数组,来储存