Don't immediately store pretty squares
The initial implementation of the code stored squares in the grid in a "pretty" form. This is a complicated bit of code and slowed down the code slightly. Now we just store the size of the current square in the grid. This reduces the code complexity, and offers a slight performance improvement. We still output a prettified version of the grid, as we can generate that from a grid populated with just square sizes.
This commit is contained in:
77
main.cc
77
main.cc
@@ -11,6 +11,7 @@
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
|
||||
/** (x, y) pair storing a position. */
|
||||
using Pos = std::pair<int, int>;
|
||||
@@ -76,35 +77,9 @@ struct Grid {
|
||||
|
||||
/** Add a square to the grid. */
|
||||
auto add(Square const &sq) noexcept -> void {
|
||||
switch (sq.length()) {
|
||||
case 1: set(sq.x(), sq.y(), '*');
|
||||
break;
|
||||
case 2: set(sq.x(), sq.y(), '+');
|
||||
set(sq.x() + 1, sq.y(), '+');
|
||||
set(sq.x(), sq.y() + 1, '+');
|
||||
set(sq.x() + 1, sq.y() + 1, '+');
|
||||
break;
|
||||
default: {
|
||||
auto n = sq.length();
|
||||
set(sq.x(), sq.y(), '+');
|
||||
set(sq.x() + n - 1, sq.y(), '+');
|
||||
set(sq.x(), sq.y() + n - 1, '+');
|
||||
set(sq.x() + n - 1, sq.y() + n - 1, '+');
|
||||
for (int i = 1; i < n - 1; ++i) {
|
||||
set(sq.x() + i, sq.y(), '-');
|
||||
set(sq.x() + i, sq.y() + n - 1, '-');
|
||||
set(sq.x(), sq.y() + i, '|');
|
||||
for (int j = 1; j < n - 1; ++j) {
|
||||
set(sq.x() + j, sq.y() + i, ' ');
|
||||
}
|
||||
set(sq.x() + n - 1, sq.y() + i, '|');
|
||||
}
|
||||
|
||||
int i = sq.x() + n - 1;
|
||||
while (n != 0) {
|
||||
set(--i, sq.y() + 1, '0' + (n % 10));
|
||||
n /= 10;
|
||||
}
|
||||
for (auto i = sq.x(); i < sq.x() + sq.length(); ++i) {
|
||||
for (auto j = sq.y(); j < sq.y() + sq.length(); ++j) {
|
||||
set(i, j, sq.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,6 +93,15 @@ struct Grid {
|
||||
}
|
||||
}
|
||||
|
||||
auto prettify() noexcept -> void {
|
||||
for (auto idx = 0; idx < grid_.size(); ++idx) {
|
||||
if (grid_[idx] < 32) {
|
||||
auto const pos = std::make_pair(idx % length_, idx / length_);
|
||||
prettify_sq(Square(pos, grid_[idx]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Output the grid. */
|
||||
auto output() const -> void {
|
||||
for (auto idx = 0; idx < length_ * length_; idx += length_) {
|
||||
@@ -162,6 +146,40 @@ struct Grid {
|
||||
}
|
||||
|
||||
private:
|
||||
auto prettify_sq(Square const &sq) noexcept -> void {
|
||||
switch (sq.length()) {
|
||||
case 1: set(sq.x(), sq.y(), '*');
|
||||
break;
|
||||
case 2: set(sq.x(), sq.y(), '+');
|
||||
set(sq.x() + 1, sq.y(), '+');
|
||||
set(sq.x(), sq.y() + 1, '+');
|
||||
set(sq.x() + 1, sq.y() + 1, '+');
|
||||
break;
|
||||
default: {
|
||||
auto n = sq.length();
|
||||
set(sq.x(), sq.y(), '+');
|
||||
set(sq.x() + n - 1, sq.y(), '+');
|
||||
set(sq.x(), sq.y() + n - 1, '+');
|
||||
set(sq.x() + n - 1, sq.y() + n - 1, '+');
|
||||
for (int i = 1; i < n - 1; ++i) {
|
||||
set(sq.x() + i, sq.y(), '-');
|
||||
set(sq.x() + i, sq.y() + n - 1, '-');
|
||||
set(sq.x(), sq.y() + i, '|');
|
||||
for (int j = 1; j < n - 1; ++j) {
|
||||
set(sq.x() + j, sq.y() + i, ' ');
|
||||
}
|
||||
set(sq.x() + n - 1, sq.y() + i, '|');
|
||||
}
|
||||
|
||||
int i = sq.x() + n - 1;
|
||||
while (n != 0) {
|
||||
set(--i, sq.y() + 1, '0' + (n % 10));
|
||||
n /= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Set the grid position (x, y) to the character c.
|
||||
*
|
||||
* It is an error if (x, y) is already set to c - as that means we have overlapping
|
||||
@@ -229,6 +247,7 @@ auto find_solution(int n) -> Grid {
|
||||
Avail avail_sqs;
|
||||
for (auto i = 0; i <= n; ++i) { avail_sqs.push_back(i); }
|
||||
find_solution_impl(grid, n, std::make_pair(0, 0), avail_sqs);
|
||||
grid.prettify();
|
||||
return grid;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user