Hello. I was reading http://xkcd.com/287/ while bored and thus decided to write a program that would solve such np complete problems. Here is what I got:
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
T NPMenuProblem(T desired, vector<T> options, vector<T> &ret)
{
vector<T> save=ret;
T remainder;
T min=desired;
vector<T> minsol;
for (int i=0; i<options.size(); ++i)
{
ret=save;
if (desired-options[i]>0.0)
{
ret.push_back(options[i]);
remainder=NPMenuProblem(desired-options[i],options,ret);
if (remainder==0)
return 0;
else if (remainder<min)
{
minsol=ret;
min=remainder;
}
}
}
ret=minsol;
return min;
}
int main()
{
vector<int> opts;
opts.push_back(215);
opts.push_back(275);
opts.push_back(335);
opts.push_back(355);
opts.push_back(420);
opts.push_back(580);
vector<int> ret;
int des=1505;
int rem=NPMenuProblem(des,opts,ret);
for (int i=0; i<ret.size(); ++i)
cout<<ret[i]<<" + ";
cout<<" = "<<des<<" - "<<rem<<endl;
return 0;
}
The issue is that it doesn't work and I cannot figure out why... any advice?