Skip to content

algo(geometry): add polyominoes #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ In this repository, you'll find algorithms and data structure implementations fo
- Geometry
- [convex hull](/algorithms/geometry/convex-hull.cpp)
- [point to segment](/algorithms/geometry/point-to-segment.cpp)
- [polyominoes](/algorithms/geometry/polyominoes.cpp)

- Graphs
- [articulation points](/algorithms/graphs/articulation-points.cpp)
Expand Down
85 changes: 85 additions & 0 deletions algorithms/geometry/polyominoes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// We consider the rotations
// as distinct (0, 10, 10+9, 10+9+8...)
vi pos = {0, 10, 19, 27, 34, 40, 45, 49, 52, 54, 55};

const int MAXP = 10;

struct Poly {
ii v[MAXP];
int64_t id;
int n;

Poly() {
n = 1;
v[0] = {0, 0};
normalize();
}

Poly(vii &vp) {
n = vp.size();
for (int i = 0; i < n; i++) v[i] = vp[i];
normalize();
}

ii &operator[](int i) { return v[i]; }

bool add(int a, int b) {
for (int i = 0; i < n; i++) {
auto [f, s] = v[i];
if (f == a and s == b) return false;
}

v[n++] = ii{a, b};
normalize();
return true;
}

void normalize() {
int mx = 100, my = 100;
for (int i = 0; i < n; i++) {
auto [f, s] = v[i];
mx = min(mx, f), my = min(my, s);
}

id = 0;
for (int i = 0; i < n; i++) {
auto &[f, s] = v[i];
f -= mx, s -= my;
id |= (1LL << (pos[f] + s));
}
}

bool operator<(Poly oth) { return id < oth.id; }
};

vector<Poly> poly[MAXP + 1];

void buildPoly(int mxN) {
for (int i = 0; i <= mxN; i++) poly[i].clear();

Poly init;
queue<Poly> q;
unordered_set<int64_t> used;
q.push(init);
used.insert(init.id);
while (not q.empty()) {
Poly u = q.front();
q.pop();
poly[u.n].emplace_back(u);

if (u.n == mxN) continue;

for (int i = 0; i < u.n; i++) {
for (auto [dx, dy] : dir4) {
Poly to = u;
auto [f, s] = to[i];
bool ok = to.add(f + dx, s + dy);

if (ok and not used.count(to.id)) {
q.push(to);
used.insert(to.id);
}
}
}
}
}
5 changes: 5 additions & 0 deletions algorithms/geometry/polyominoes.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
\subsection{Polynominoes}

Geometric figure made by equal squares, connected between themselves in a way that at least one side of each square coincide with a side of another square.

Watch out: the number of polynominoes increases fastly (size 12 has 63.600 figures)
Binary file modified notebook.pdf
Binary file not shown.