Module 14: Find Spans in the Grid
This commit is contained in:
parent
68cbfe9663
commit
0f7020f74c
90
a.c
90
a.c
@ -44,6 +44,7 @@ struct Span {
|
||||
int len;
|
||||
bool vert;
|
||||
};
|
||||
typedef vector<Span> Spans; // No need for pointers, as spans are not so numerous
|
||||
|
||||
ostream& operator<<(ostream& os, const Span& s) {
|
||||
os << "[" << s.point << " len=" << s.len << " vert=" << s.vert << "]";
|
||||
@ -171,6 +172,72 @@ struct Grid {
|
||||
return lines[0].size();
|
||||
}
|
||||
}
|
||||
// Returns character value of the box at point 'p'
|
||||
// 'p' must be in bounds
|
||||
char box(const Point& p) const {
|
||||
assert(in_bounds(p));
|
||||
return lines[p.row][p.col];
|
||||
}
|
||||
// Returns true if point p is a '.' "block" in the grid
|
||||
// 'p' must be in bounds
|
||||
bool is_block(const Point& p) const {
|
||||
return box(p) == '.';
|
||||
}
|
||||
bool is_blank(const Point& p) const {
|
||||
return box(p) == '-';
|
||||
}
|
||||
bool is_letter(const Point& p) const {
|
||||
char c = box(p);
|
||||
return c >= 'A' && c <= 'Z';
|
||||
}
|
||||
bool in_bounds(const Point& p) const {
|
||||
return (p.row >= 0 && p.row < rows() && p.col >= 0 && p.col < cols());
|
||||
}
|
||||
// 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) {
|
||||
if (vert) {
|
||||
p.row++;
|
||||
if (p.row >= rows()) {
|
||||
p.row = 0;
|
||||
p.col++;
|
||||
}
|
||||
} else {
|
||||
p.col++;
|
||||
if (p.col >= cols()) {
|
||||
p.col = 0;
|
||||
p.row++;
|
||||
}
|
||||
}
|
||||
return in_bounds(p);
|
||||
}
|
||||
void FillSpans(bool vert) {
|
||||
Point p;
|
||||
// check all spans
|
||||
while (in_bounds(p)) {
|
||||
// for each span
|
||||
while (in_bounds(p) && is_block(p)) {
|
||||
Next(p, vert);
|
||||
}
|
||||
if (!in_bounds(p)) return;
|
||||
Point startp = p;
|
||||
// cout << "SPAN START: " << p << "\n";
|
||||
|
||||
int len = 0;
|
||||
do {
|
||||
Next(p, vert);
|
||||
len++;
|
||||
} while (in_bounds(p) && !is_block(p));
|
||||
//cout << "END OF SPAN!!! len=" << len << "\n";
|
||||
spans.push_back(Span(startp, len, vert));
|
||||
}
|
||||
}
|
||||
// Add to 'spans' vector with all viable spans in the grid
|
||||
void FillSpans() {
|
||||
assert(spans.empty());
|
||||
FillSpans(false); // horiz
|
||||
FillSpans(true); // vert
|
||||
}
|
||||
void LoadFromFile(string filename) {
|
||||
ifstream f;
|
||||
f.open("test");
|
||||
@ -196,8 +263,15 @@ struct Grid {
|
||||
cout << " " << s << "\n";
|
||||
}
|
||||
}
|
||||
void PrintSpans() const {
|
||||
cout << "Spans:\n";
|
||||
for (const Span& s : spans) {
|
||||
cout << " " << s << "\n";
|
||||
}
|
||||
}
|
||||
string name; // strings are initialized empty
|
||||
vector<string> lines;
|
||||
Spans spans;
|
||||
};
|
||||
|
||||
int main() {
|
||||
@ -208,18 +282,6 @@ int main() {
|
||||
grid.LoadFromFile("test");
|
||||
grid.Check();
|
||||
grid.Print();
|
||||
|
||||
Point p1;
|
||||
Point p2(2,1);
|
||||
|
||||
cout << "Point1 is " << p1 << "\n";
|
||||
cout << "Point2 is " << p2 << "\n";
|
||||
|
||||
Span s1(p1, 3, true);
|
||||
Span s2(p2, 5, false);
|
||||
|
||||
cout << "Span1 is " << s1 << "\n";
|
||||
cout << "Span2 is " << s2 << "\n";
|
||||
|
||||
|
||||
grid.FillSpans();
|
||||
grid.PrintSpans();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user