Module 17: Write Words to the Grid
This commit is contained in:
parent
25d683ffef
commit
e944bd3cbb
80
a.c
80
a.c
@ -8,6 +8,8 @@ using namespace std;
|
||||
|
||||
// For compiling C++ code
|
||||
// g++ a.c -o a
|
||||
// with optimizations
|
||||
// g++ a.c -O2 -o a
|
||||
|
||||
string ToUpper(string s) {
|
||||
string s2;
|
||||
@ -139,14 +141,13 @@ public:
|
||||
while (f.is_open() && !f.eof()) { // check for the file!
|
||||
string line;
|
||||
getline(f, line);
|
||||
// cout << line << "\n";
|
||||
if (!line.empty()) {
|
||||
line = ToUpper(line);
|
||||
int len = line.length();
|
||||
if (line[len - 1] == '\r') {
|
||||
line = line.substr(0, len - 1);
|
||||
}
|
||||
if (len <= max_size) {
|
||||
if (line.length() <= max_size) {
|
||||
Word* w = new Word(line);
|
||||
words_.push_back(w); // Word would be allocated on the heap
|
||||
CreatePatternHash(w);
|
||||
@ -202,6 +203,10 @@ struct Grid {
|
||||
assert(in_bounds(p));
|
||||
return lines[p.row][p.col];
|
||||
}
|
||||
void write_box(const Point& p, char c) {
|
||||
assert(in_bounds(p));
|
||||
lines[p.row][p.col] = c;
|
||||
}
|
||||
// Returns true if point p is a '.' "block" in the grid
|
||||
// 'p' must be in bounds
|
||||
bool is_block(const Point& p) const {
|
||||
@ -234,6 +239,14 @@ struct Grid {
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
void WriteString(const Span& s, const string& t) {
|
||||
int len = s.len;
|
||||
assert(t.length() == len);
|
||||
for (int i=0; i<len; i++) {
|
||||
Point p = s.GetPoint(i);
|
||||
write_box(p, t[i]);
|
||||
}
|
||||
}
|
||||
// Next increments the point across the grid, one box at a time
|
||||
// Returns true if point is still in bounds
|
||||
bool Next(Point& p, bool vert) {
|
||||
@ -359,21 +372,26 @@ ostream& operator<<(ostream& os, const Slot& s) {
|
||||
//-Solver
|
||||
class Solver {
|
||||
public:
|
||||
Solver(Grid* g) : grid_(g) {} // pointer to the grid
|
||||
void Solve() {
|
||||
Solver() {}
|
||||
void Solve(const Grid& grid) { // reference to the grid
|
||||
cout << "Solving this grid:\n";
|
||||
grid_->Print();
|
||||
Loop();
|
||||
grid.Print();
|
||||
Loop(grid, 0);
|
||||
}
|
||||
|
||||
private:
|
||||
void Loop() {
|
||||
void Loop(Grid grid, int depth) { // full copy of the grid to allow recursion
|
||||
depth++;
|
||||
// if (depth > 3) {
|
||||
// cout << "Aborting loop because depth=" << depth << "\n";
|
||||
// return;
|
||||
// }
|
||||
Slots empty_slots; // these are the ones we want to work on
|
||||
Slots partial_slots;
|
||||
Slots full_slots;
|
||||
for (const Span& s : grid_->spans) {
|
||||
for (const Span& s : grid.spans) {
|
||||
Attr attr;
|
||||
string temp = grid_->GetString(s, attr);
|
||||
string temp = grid.GetString(s, attr);
|
||||
if (attr.is_empty()) {
|
||||
empty_slots.push_back(Slot(s, temp));
|
||||
} else if (attr.is_partial()) {
|
||||
@ -385,31 +403,43 @@ class Solver {
|
||||
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";
|
||||
// cout << "empty = " << num_empty << "\n";
|
||||
// cout << "partial = " << num_partial << "\n";
|
||||
// cout << "full = " << num_full << "\n";
|
||||
|
||||
// need to check that all words so far are valid!
|
||||
for (const Slot& s : full_slots) {
|
||||
// cout << "CHECKING " << s.pattern << " if it is a word\n";
|
||||
if (!lib.IsWord(s.pattern)) {
|
||||
// cout << " --> NOT! ABORT\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (num_partial == 0 && num_empty == 0) {
|
||||
cout << "SOLUTION!!\n";
|
||||
// FIX what do we do here?
|
||||
grid.Print();
|
||||
return;
|
||||
}
|
||||
assert(num_partial > 0);
|
||||
CommitSlot(partial_slots[0]);
|
||||
CommitSlot(grid, partial_slots[0], depth);
|
||||
}
|
||||
void CommitSlot(const Slot& slot) {
|
||||
cout << "COMMIT slot " << slot << "\n";
|
||||
cout << "Possible word choices for this slot are:\n";
|
||||
void CommitSlot(Grid& grid, const Slot& slot, int depth) {
|
||||
// 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";
|
||||
// cout << " Committing '" << w->word << "'!\n";
|
||||
grid.WriteString(slot.span, w->word);
|
||||
// cout << "New grid is:\n";
|
||||
// grid.Print();
|
||||
Loop(grid, depth); // Recursion
|
||||
}
|
||||
} else {
|
||||
// cout << "NO MATCHES to pattern\n";
|
||||
}
|
||||
}
|
||||
Grid* grid_;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
@ -422,6 +452,6 @@ int main() {
|
||||
|
||||
lib.ReadFromFile("top_12000.txt", grid.max_size());
|
||||
|
||||
Solver solver(&grid); // address pointer to the grid
|
||||
solver.Solve();
|
||||
Solver solver;
|
||||
solver.Solve(grid);
|
||||
}
|
Loading…
Reference in New Issue
Block a user