【问题标题】:Is there a solution instead of move for knapsack backtracking algorithm in C++C++中的背包回溯算法是否有解决方案而不是移动
【发布时间】:2016-05-28 20:35:19
【问题描述】:

我使用 C++ 的 Backtracking of Knapsack 解决方案。我又从这里得到了解决方案。 Knapsack solution with Backtraking in c++ 我的编译器在行移动时出错。有没有不使用移动的解决方案?不使用move如何修改代码?

#include<iostream>
#include<vector>

using namespace std;

int weights[] = {2, 3, 1}, values[] = {6, 15, 7};

int solution = 0, n = 3;

std::vector<int> vsol;
std::vector<int> temp;

bool issol;


void Knapsack (int i, int max, int value)
{
  for (int k = i; k < n; k++) {
    if ( max > 0)
    {
        if (weights[k] <= max)
        {
          temp.push_back(k);
          if (value+ values[k] >= solution)
          {
            solution = value + values[k];
            issol = true;
          }
        }
        if ( (k+1) < n)
        {
          Knapsack (k+1, max - weights[k], value + values[k]);
        }
        else
        {
          if (issol == true)
          {
            if (! vsol.empty()) vsol.clear();
            std::move(temp.begin(), temp.end(), std::back_inserter(vsol));
            temp.clear();
            issol = false;
          } else temp.clear();
          return;
        }
    }
    else
    {
        if (issol == true)
        {
            if (! vsol.empty()) vsol.clear();
            std::move(temp.begin(), temp.end(), std::back_inserter(vsol));
            temp.clear();
            issol = false;
        } else temp.clear();
        return;
    }
  }
}

int main()
{
    Knapsack(0, 2, 0);
    cout << "solution: " << solution << endl;
    for(vector<int>::iterator it = vsol.begin(); it != vsol.end(); it++)
        cout << *it << " ";
    return 0;
}

【问题讨论】:

  • 您是否加入了&lt;algorithm&gt;?您是否在编译器中启用了 C++11 模式?
  • 是的,我也尝试了实用程序,但没有任何区别。
  • edit 你的问题提供minimal reproducible example(如果你想让move 工作,主要是)。
  • 我编辑了我的问题。我试图解释得更清楚。我希望现在没事。 @鲍姆米特奥根

标签: c++ c++11 c++builder backtracking knapsack-problem


【解决方案1】:

您可以将std::move 更改为std::copy。一般来说,它们并不等效,但在这里您使用的是int 的向量(因此没有性能损失)并且源数组会立即被清除(temp.clear())。

所以你可以写:

if (!vsol.empty()) vsol.clear();
std::copy(temp.begin(), temp.end(), std::back_inserter(vsol));

确实是想多了。很简单:

vsol = temp;

清晰、快速并且还可以与 C++98 一起使用(取决于您的 C++Builder 版本,C++11 兼容性可能是个问题)。

修改后的版本:

#include <iostream>
#include <vector>

int weights[] = {2, 3, 1}, values[] = {6, 15, 7};

int solution = 0, n = 3;

std::vector<int> vsol;
std::vector<int> temp;

bool issol;

void Knapsack(int i, int max, int value)
{
  for (int k = i; k < n; ++k)
  {
    if (max > 0)
    {
      if (weights[k] <= max)
      {
        temp.push_back(k);
        if (value + values[k] >= solution)
        {
          solution = value + values[k];
          issol = true;
        }
      }

      if (k + 1 < n)
      {
        Knapsack(k + 1, max - weights[k], value + values[k]);
      }
      else
      {
        if (issol)
        {
          vsol = temp;
          issol = false;
        }

        temp.clear();
        return;
      }
    }
    else
    {
      if (issol)
      {
        vsol = temp;
        issol = false;
      }

      temp.clear();
      return;
    }
  }
}

int main()
{
  Knapsack(0, 2, 0);
  std::cout << "solution: " << solution << '\n';
  for(std::vector<int>::iterator it = vsol.begin(); it != vsol.end(); ++it)
    std::cout << *it << " ";

  return 0;
}

【讨论】:

  • @xxxx 如果解决了您的问题,请采纳。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
相关资源
最近更新 更多