Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 98 lines (82 sloc) 2.199 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/*
* Author: Ricardo Garcia Gonzalez
* License: Public domain code
*/
#include <iostream>
#include <list>
#include <vector>
#include <set>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <limits>

#include "operation.h"
#include "node.h"
#include "memory_manager.h"

using std::list;
using std::vector;
using std::set;
using std::cout;
using std::cerr;
using std::endl;
using std::clock;
using std::strtol;
using std::strlen;
using std::find;
using std::sort;
using std::greater;

int main(int argc, char *argv[])
{
vector<long> basics_v;
char *endptr;
long number;

// Check number of arguments.
if (argc < 4) {
cerr << "Usage: " << argv[0] << " num num... target" << endl;
return -1;
}

// Get number list, checking argument format.
for (int ii = 1; ii < argc; ++ii) {
number = strtol(argv[ii], &endptr, 0);

if (endptr != argv[ii] + strlen(argv[ii])) {
cerr << "Error: \"" << argv[ii] << "\" not a number."
<< endl;
return -2;
}

if (number <= 0) {
cerr << "Error: numbers below or equal to zero are not "
"allowed." << endl;
return -3;
}

basics_v.push_back(number);
}

// Separate target.
long target = basics_v.back();
basics_v.pop_back();

// Check target not in numbers.
if (find(basics_v.begin(), basics_v.end(), target) != basics_v.end()) {
cerr << "Error: target present in numbers to operate." << endl;
return -4;
}

// Sort initial numbers and change to list.
sort(basics_v.begin(), basics_v.end(), greater<long>());
list<long> basics(basics_v.begin(), basics_v.end());

// Execution.
MemoryManager *mm = MemoryManager::instance();
set<list<long> > explored_nodes;
clock_t before = clock();
Node *root = mm->build_node(list<const Operation *>(), 0L, target,
basics, explored_nodes, 0);
clock_t after = clock();

// Print results.
cout << root->best();
double clocks = static_cast<double>(after - before);
double cps = static_cast<double>(CLOCKS_PER_SEC);
cout << "Elapsed time: " << (clocks / cps) << " seconds" << endl;
cout << "Explored nodes: " << explored_nodes.size() << endl;

// Free memory.
mm->free();

return 0;
}
Something went wrong with that request. Please try again.