79 lines
1.7 KiB
C++
79 lines
1.7 KiB
C++
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
|
||
|
vector<vector<tuple<char, bool>>> squares;
|
||
|
|
||
|
bool rec(int x, int y, set<pair<int, int>> ®ion) {
|
||
|
auto &[c, g] = squares[x][y];
|
||
|
if (g) {
|
||
|
return false;
|
||
|
}
|
||
|
g = true;
|
||
|
region.insert({x, y});
|
||
|
vector<pair<int, int>> checks = {
|
||
|
{x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}};
|
||
|
for (auto [xx, yy] : checks) {
|
||
|
if (xx < 0 || yy < 0 || xx >= squares.size() ||
|
||
|
yy >= squares[0].size()) {
|
||
|
continue;
|
||
|
}
|
||
|
if (get<0>(squares[xx][yy]) == c) {
|
||
|
rec(xx, yy, region);
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
ios::sync_with_stdio(0);
|
||
|
cin.tie(0);
|
||
|
|
||
|
string line;
|
||
|
while (getline(cin, line) && !line.empty()) {
|
||
|
vector<tuple<char, bool>> l;
|
||
|
for (char c : line) {
|
||
|
l.push_back({c, false});
|
||
|
}
|
||
|
squares.push_back(l);
|
||
|
}
|
||
|
|
||
|
vector<set<pair<int, int>>> regions;
|
||
|
for (int x = 0; x < squares.size(); x++) {
|
||
|
for (int y = 0; y < squares[0].size(); y++) {
|
||
|
set<pair<int, int>> region;
|
||
|
if (rec(x, y, region)) {
|
||
|
regions.push_back(region);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int total = 0;
|
||
|
for (auto ®ion : regions) {
|
||
|
map<pair<int, int>, bool[4]> sides;
|
||
|
int area = region.size();
|
||
|
for (auto [x, y] : region) {
|
||
|
vector<pair<int, int>> directions = {
|
||
|
{x - 1, y}, {x + 1, y}, {x, y - 1}, {x, y + 1}};
|
||
|
for (int i = 0; i < 4; i++) {
|
||
|
sides[{x, y}][i] = !region.contains(directions[i]);
|
||
|
}
|
||
|
}
|
||
|
int sidec = 0;
|
||
|
for (auto &[p, b] : sides) {
|
||
|
auto [x, y] = p;
|
||
|
for (int i = 0; i < 4; i++) {
|
||
|
if (!b[i]) {
|
||
|
continue;
|
||
|
}
|
||
|
pair<int, int> other =
|
||
|
(i < 2) ? make_pair(x, y - 1) : make_pair(x - 1, y);
|
||
|
if (!sides.contains(other) || !sides[other][i]) {
|
||
|
sidec++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
total += area * sidec;
|
||
|
}
|
||
|
cout << total << '\n';
|
||
|
}
|