動態規劃解決01揹包問題

/*本程序旨在利用動態規劃解決有價值的0-1揹包問題*/
/*物品的大小和價值以及揹包的大小保存在一個放置在E盤根目錄下的的csv文件中*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    ifstream in("E://packageProblem.csv");
    string line;
    string field;
    int packageSize;
    int objectNum;
    getline(in, line);
    stringstream ss;
    istringstream sin;
    ss << line;
    ss >> packageSize;
    ss.clear();
    getline(in, line);
    ss << line;
    ss >> objectNum;
    ss.clear();
    int *objectSize = new int[objectNum];
    int *objectValue = new int[objectNum];
    int sizePointer = 0;
    int valuePointer = 0;
    while (getline(in, line))
    {
        cout<<line<<endl;
        sin.str(line);
        getline(sin, field, ',');
        ss << field;
        cout<<"size:"<<field<<" ";
        ss >> objectSize[sizePointer];
        ss.clear();
        sizePointer++;
        getline(sin, field, ',');
        ss << field;
        cout<<"value:"<<field<<endl;
        ss >> objectValue[valuePointer];
        ss.clear();
        sin.clear();
        valuePointer++;
    }
    //讀入數據的部分結束了
    //行是物品件數(0-objectNum),列是揹包大小(0-packageSize)
    int **table = new int *[objectNum + 1];
    for (int i = 0; i < objectNum + 1; i++)
    {
        table[i] = new int[packageSize + 1];
    }
    //初始化,第一行和第一列是0
    for (int i = 0; i < objectNum + 1; i++)
    {
        table[i][0] = 0;
    }
    for (int j = 0; j < packageSize + 1; j++)
    {
        table[0][j] = 0;
    }
    //開始寫表
    for (int i = 1; i < objectNum + 1; i++)
    {
        for (int j = 1; j < packageSize + 1; j++)
        {
            if (j < objectSize[i - 1]){
                //若是放不下第i件物品,就直接不放
                table[i][j] = table[i - 1][j];
                //cout<<"table["<<i<<"]["<<j<<"]="<<table[i][j]<<endl;   
            }
            else{
                //比較要上以後該物品的值+跳轉以後的值和不要該物品的值哪一個大
                table[i][j] = max(objectValue[i - 1] + table[i-1][j - objectSize[i - 1]], table[i - 1][j]);
                //cout<<"table["<<i<<"]["<<j<<"]="<<table[i][j]<<endl;
            } 
        }
    }
    //表寫完了,開始從末尾讀表
    int i = objectNum;
    int j = packageSize;
    cout << "總價值爲" << table[i][j] << endl;
    while (i > 0 && j > 0)
    {
        if (table[i][j] == table[i - 1][j])
        {
            cout << "第" << i << "件物品沒拿" << endl;
            i--;
        }
        else
        {
            cout << "第" << i << "件物品拿了" << endl;
            j -= objectSize[i-1];
            i--;
        }
    }
    while(j==0&&i>0){
        cout<<"第" << i << "件物品沒拿" << endl;
        i--;
    }
    cout<<endl;
    return 0;
}
相關文章
相關標籤/搜索