Module 16: Solver Class
This commit is contained in:
parent
ee92bef817
commit
b587e75022
119
a.c
119
a.c
@ -79,13 +79,13 @@ public:
|
||||
delete w;
|
||||
}
|
||||
}
|
||||
void FindWord(const string& s) const { // references are prefered instead of copies
|
||||
// Returns NULL if can't find any matches to the given /pattern
|
||||
const Words* FindWord(const string& s) const { // references are prefered instead of copies
|
||||
auto it = word_map_.find(s);
|
||||
if (it != word_map_.end()) {
|
||||
for (const Word* w : it->second) {
|
||||
cout << " " << w->word;
|
||||
}
|
||||
cout << "\n";
|
||||
return &it->second; // address of the vector of words
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
bool IsWord(string s) const {
|
||||
@ -168,6 +168,21 @@ private: // _ is used to indicate privacy
|
||||
vector<int> counts_;
|
||||
};
|
||||
|
||||
Library lib; // not ideal, but it is not so bad for this application
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
//-Attr
|
||||
struct Attr {
|
||||
bool is_empty() const { return has_blanks && !has_letters; }
|
||||
bool is_partial() const { return has_blanks && has_letters; }
|
||||
bool is_full() const { return !has_blanks && has_letters; }
|
||||
|
||||
bool has_letters = false;
|
||||
bool has_blanks = false;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
//-Grid
|
||||
struct Grid {
|
||||
Grid(string n) {
|
||||
name = n;
|
||||
@ -203,12 +218,18 @@ struct Grid {
|
||||
return (p.row >= 0 && p.row < rows() && p.col >= 0 && p.col < cols());
|
||||
}
|
||||
// Fills in attributes of the string
|
||||
string GetString(const Span& s) const {
|
||||
string GetString(const Span& s, Attr& attr) const {
|
||||
int len = s.len;
|
||||
string temp;
|
||||
temp.resize(len);
|
||||
for (int i=0; i<len; i++) {
|
||||
Point p = s.GetPoint(i);
|
||||
char c = box(p);
|
||||
if (c == '-') {
|
||||
attr.has_blanks = true;
|
||||
} else if (c >= 'A' && c <= 'Z') {
|
||||
attr.has_letters = true;
|
||||
}
|
||||
temp[i] = box(p);
|
||||
}
|
||||
return temp;
|
||||
@ -309,7 +330,8 @@ struct Grid {
|
||||
void PrintSpans() const {
|
||||
cout << "Spans:\n";
|
||||
for (const Span& s : spans) {
|
||||
cout << " " << s << " " << GetString(s) << "\n";
|
||||
Attr attr;
|
||||
cout << " " << s << " " << GetString(s, attr) << "\n";
|
||||
}
|
||||
}
|
||||
string name; // strings are initialized empty
|
||||
@ -317,14 +339,89 @@ struct Grid {
|
||||
Spans spans;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
//-Slot
|
||||
struct Slot {
|
||||
Slot(const Span s, const string& p) : span(s), pattern(p) {}
|
||||
friend ostream& operator<<(ostream& os, const Slot& s);
|
||||
|
||||
Span span;
|
||||
string pattern;
|
||||
};
|
||||
typedef vector<Slot> Slots;
|
||||
|
||||
ostream& operator<<(ostream& os, const Slot& s) {
|
||||
os << s.span << " '" << s.pattern << "'";
|
||||
return os;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
//-Solver
|
||||
class Solver {
|
||||
public:
|
||||
Solver(Grid* g) : grid_(g) {} // pointer to the grid
|
||||
void Solve() {
|
||||
cout << "Solving this grid:\n";
|
||||
grid_->Print();
|
||||
Loop();
|
||||
}
|
||||
|
||||
private:
|
||||
void Loop() {
|
||||
Slots empty_slots; // these are the ones we want to work on
|
||||
Slots partial_slots;
|
||||
Slots full_slots;
|
||||
for (const Span& s : grid_->spans) {
|
||||
Attr attr;
|
||||
string temp = grid_->GetString(s, attr);
|
||||
if (attr.is_empty()) {
|
||||
empty_slots.push_back(Slot(s, temp));
|
||||
} else if (attr.is_partial()) {
|
||||
partial_slots.push_back(Slot(s, temp));
|
||||
} else if (attr.is_full()) {
|
||||
full_slots.push_back(Slot(s, temp));
|
||||
}
|
||||
}
|
||||
int num_empty = empty_slots.size();
|
||||
int num_partial = partial_slots.size();
|
||||
int num_full = full_slots.size();
|
||||
cout << "empty = " << num_empty << "\n";
|
||||
cout << "partial = " << num_partial << "\n";
|
||||
cout << "full = " << num_full << "\n";
|
||||
if (num_partial == 0 && num_empty == 0) {
|
||||
cout << "SOLUTION!!\n";
|
||||
// FIX what do we do here?
|
||||
}
|
||||
assert(num_partial > 0);
|
||||
CommitSlot(partial_slots[0]);
|
||||
}
|
||||
void CommitSlot(const Slot& slot) {
|
||||
cout << "COMMIT slot " << slot << "\n";
|
||||
cout << "Possible word choices for this slot are:\n";
|
||||
|
||||
const Words* words = lib.FindWord(slot.pattern);
|
||||
if (words) {
|
||||
for (const Word* w : *words) {
|
||||
cout << " " << w->word;
|
||||
}
|
||||
cout << "\n";
|
||||
} else {
|
||||
cout << "NO MATCHES to pattern\n";
|
||||
}
|
||||
}
|
||||
Grid* grid_;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
int main() {
|
||||
Grid grid("MY GRID");
|
||||
Grid grid("MY GRID"); // grid lives on the stack
|
||||
grid.LoadFromFile("test");
|
||||
grid.Check();
|
||||
grid.Print();
|
||||
grid.FillSpans();
|
||||
grid.PrintSpans();
|
||||
|
||||
Library lib;
|
||||
|
||||
lib.ReadFromFile("top_12000.txt", grid.max_size());
|
||||
|
||||
Solver solver(&grid); // address pointer to the grid
|
||||
solver.Solve();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user