This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
constexpr int N = 25;
char hor[N][N], ver[N][N];
bool vis[N][N];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
for (int i = 0; i <= n; i++) {
for (int j = 0; j < m; j++) {
std::cin >> hor[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= m; j++) {
std::cin >> ver[i][j];
}
}
std::vector<int> a;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vis[i][j])
continue;
int Size = 0;
std::queue<std::pair<int, int>> que;
que.emplace(i, j);
vis[i][j] = true;
while (!que.empty()) {
auto [x, y] = que.front();
que.pop();
Size++;
// std::cout << x << " " << y << "\n";
if (x > 0 && hor[x][y] == '0' && !vis[x - 1][y]) {
que.emplace(x - 1, y);
vis[x - 1][y] = true;
}
if (x + 1 < n && hor[x + 1][y] == '0' && !vis[x + 1][y]) {
que.emplace(x + 1, y);
vis[x + 1][y] = true;
}
if (y > 0 && ver[x][y] == '0' && !vis[x][y - 1]) {
que.emplace(x, y - 1);
vis[x][y - 1] = true;
}
if (y + 1 < m && ver[x][y + 1] == '0' && !vis[x][y + 1]) {
que.emplace(x, y + 1);
vis[x][y + 1] = true;
}
}
if (Size == 1 && hor[i][j] == '1' && hor[i + 1][j] == '1' && ver[i][j] == '1' && ver[i][j + 1] == '1')
continue;
a.emplace_back(Size);
// std::cout << Size << "\n";
}
}
int k = a.size();
int pw = 1;
for (int i = 0; i < k; i++, pw *= 3);
std::function<int(std::vector<int>)> calc = [&](std::vector<int> v) {
int ans = -1e9;
if (std::count(v.begin(), v.end(), 2) == k) {
return 0;
}
for (int i = 0; i < k; i++) {
if (v[i] == 0) {
v[i] = 1;
ans = std::max(ans, -calc(v));
v[i] = 0;
} else if (v[i] == 1) {
v[i] = 2;
ans = std::max(ans, a[i] + calc(v));
v[i] = 1;
}
}
return ans;
};
// std::cout << calc({1, 1, 0, 1, 0}) << "\n";
std::cout << calc(std::vector<int>(k)) << "\n";
return 0;
}
//
// + 1
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |