Skip to content

Commit

Permalink
Add subgoals and matrix stats
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzm committed May 16, 2019
1 parent 252dc53 commit ac39145
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 16 deletions.
13 changes: 12 additions & 1 deletion Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ Header* Matrix::findColumn(string const& name, bool isPrimary)
if (c == nullptr) {
c = new Header(name);
columns[name] = c;
if (isPrimary) c->insertLR(&h);
if (isPrimary) {
c->insertLR(&h);
++primaryCols;
}
++totalCols;
}
return c;
}


void Matrix::getColumnStats(int& primary, int& total)
{
primary = primaryCols;
total = totalCols;
}


void Matrix::findCovers(
int &nodeCount,
int &solutionCount,
Expand Down
3 changes: 3 additions & 0 deletions Matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Matrix
Matrix();

Header* findColumn(std::string const& name, bool isPrimary=true);
void getColumnStats(int& primary, int& total);

void findCovers(
int &nodeCount,
Expand All @@ -39,6 +40,8 @@ class Matrix

Header h;
std::map<std::string, Header*> columns;
int primaryCols;
int totalCols;

};

Expand Down
13 changes: 12 additions & 1 deletion Pentominoes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ void Pentominoes::addAspectPlacements(
}


void Pentominoes::init(Matrix& matrix)
void Pentominoes::init(int& rows, int& primaryCols, int& totalCols, int& elems)
{
subGoals.emplace_back();
auto& matrix = subGoals.back();

set<Piece> aspects;
for(auto const& piece: pieces) {
addPieceAspects(piece, aspects);
Expand All @@ -168,15 +171,23 @@ void Pentominoes::init(Matrix& matrix)
auto c = matrix.findColumn(placement.name);
auto e = new Element();
e->insertUD(c);
++elems;
for(auto const& cell: placement.cells) {
ostringstream cname;
cname << setfill('0') << setw(2) << cell.rOffset << setw(2) << cell.cOffset;
auto c2 = matrix.findColumn(cname.str());
auto e2 = new Element();
e2->insertUD(c2);
e2->insertLR(e);
++elems;
++rows;
}
}

int primary, total;
matrix.getColumnStats(primary, total);
primaryCols += primary;
totalCols += total;
}


Expand Down
2 changes: 1 addition & 1 deletion Pentominoes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Pentominoes : public Puzzle
{
protected:

void init(Matrix& matrix) override;
void init(int& rows, int& primaryCols, int& totalCols, int& elem) override;
void print(std::vector<Element *>& solution) override;

virtual void buildBoard(std::set<Cell>& board) = 0;
Expand Down
26 changes: 17 additions & 9 deletions Puzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ Puzzle::~Puzzle()

void Puzzle::solve(bool countOnly)
{
Matrix matrix;
init(matrix);
int nodeCount = 0;
int solutionCount = 0;
matrix.findCovers(nodeCount, solutionCount, countOnly
? function<void (vector<Element *>&)>()
: [this](vector<Element *>& solution){ print(solution); }
);
cout << solutionCount << " solutions, " << nodeCount << " nodes visted" << endl;
int rows = 0, primaryCols = 0, totalCols = 0, elems = 0;
init(rows, primaryCols, totalCols, elems);

int nodeCount = 0, solutionCount = 0;
for(auto &matrix: subGoals) {
matrix.findCovers(nodeCount, solutionCount, countOnly
? function<void (vector<Element *>&)>()
: [this](vector<Element *>& solution){ print(solution); }
);
}

cout << subGoals.size() << " subgoals, ";
cout << rows << "x" << totalCols << " constraint matrix, ";
cout << primaryCols << " primary columns, ";
cout << elems << " elements" << endl;
cout << solutionCount << " solutions, ";
cout << nodeCount << " search nodes visted" << endl;
}
7 changes: 5 additions & 2 deletions Puzzle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

#include <vector>

#include "Matrix.hpp"

class Element;
class Matrix;

class Puzzle
{
Expand All @@ -16,9 +17,11 @@ class Puzzle

protected:

virtual void init(Matrix& matrix) = 0;
virtual void init(int& rows, int& primaryCols, int& totalCols, int& elems) = 0;
virtual void print(std::vector<Element *>& solution) = 0;

std::vector<Matrix> subGoals;

};

#endif // !defined(PUZZLE_H)
21 changes: 20 additions & 1 deletion Queens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,36 @@ Queens::Queens(int n)
}


void Queens::init(Matrix& matrix)
void Queens::init(int& rows, int& primaryCols, int& totalCols, int& elems)
{
subGoals.emplace_back();
auto& matrix = subGoals.back();

int r = (n-1)/2, rs = -1;
for(int i=0; i<n; ++i) {
r += (rs*i);
rs *= -1;

int f = (n-1)/2, fs = -1;
for(int j=0; j<n; ++j) {
f += (fs*j);
fs *= -1;

ostringstream rname;
rname << "R" << r;
auto e1 = new Element();
auto c1 = matrix.findColumn(rname.str());
e1->insertUD(c1);
++elems;

ostringstream fname;
fname << "F" << f;
auto e2 = new Element();
auto c2 = matrix.findColumn(fname.str());
e2->insertUD(c2);
e2->insertLR(e1);
++elems;

int a = r+f;
if (a!=0 && a!=(2*n-2)) {
ostringstream aname;
Expand All @@ -47,7 +56,9 @@ void Queens::init(Matrix& matrix)
auto c3 = matrix.findColumn(aname.str(), false);
e3->insertUD(c3);
e3->insertLR(e1);
++elems;
}

int b = n - 1 - r + f;
if (b!=0 && b!=(2*n-2)) {
ostringstream bname;
Expand All @@ -56,9 +67,17 @@ void Queens::init(Matrix& matrix)
auto c4 = matrix.findColumn(bname.str(), false);
e4->insertUD(c4);
e4->insertLR(e1);
++elems;
}

++rows;
}
}

int primary, total;
matrix.getColumnStats(primary, total);
primaryCols += primary;
totalCols += total;
}


Expand Down
2 changes: 1 addition & 1 deletion Queens.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Queens : public Puzzle

private:

void init(Matrix& matrix) override;
void init(int& rows, int& primaryCols, int& totalCols, int& elem) override;
void print(std::vector<Element *>& solution) override;

int n;
Expand Down

0 comments on commit ac39145

Please sign in to comment.