66 lines
1.3 KiB
C++
66 lines
1.3 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) {
|
||
|
int area = region.size();
|
||
|
int perim = 0;
|
||
|
for (auto [x, y] : region) {
|
||
|
for (auto &d : vector<pair<int, int>>(
|
||
|
{{x - 1, y}, {x, y - 1}, {x + 1, y}, {x, y + 1}})) {
|
||
|
if (!region.contains(d)) {
|
||
|
perim++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
total += area * perim;
|
||
|
}
|
||
|
cout << total << '\n';
|
||
|
}
|