#include <iostream>
#include <vector>
#include <algorithm>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
const int infinity = 1e9;
int n, m;
vvb hor_edge, ver_edge;
inline int Val(int i, int j) {
return i + j * n;
}
inline bool Inside(int i, int j) {
if (i < 0 || i >= n || j < 0 || j >= m) return false;
return hor_edge[i][j] && hor_edge[i + 1][j] && ver_edge[i][j] && ver_edge[i][j + 1];
}
int Solve(bool player1, int sa, int st) {
int ans = player1 ? -infinity : infinity;
for (int i = 0; i <= n; i++) {
for (int j = 0; j < m; j++) {
if (!hor_edge[i][j]) {
hor_edge[i][j] = true;
bool b1 = Inside(i, j);
bool b2 = Inside(i - 1, j);
if (player1) sa += b1 + b2;
else st += b1 + b2;
int val = Solve((b1 || b2) ? player1 : !player1, sa, st);
if (player1) {
sa -= b1 + b2;
chkmax(ans, val);
}
else {
st -= b1 + b2;
chkmin(ans, val);
}
hor_edge[i][j] = false;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= m; j++) {
if (!ver_edge[i][j]) {
ver_edge[i][j] = true;
bool b1 = Inside(i, j);
bool b2 = Inside(i, j - 1);
if (player1) sa += b1 + b2;
else st += b1 + b2;
int val = Solve((b1 || b2) ? player1 : !player1, sa, st);
if (player1) {
sa -= b1 + b2;
chkmax(ans, val);
} else {
st -= b1 + b2;
chkmin(ans, val);
}
ver_edge[i][j] = false;
}
}
}
if (ans == -infinity || ans == infinity) {
if (sa == st) {
cout << "";
}
return sa - st;
}
return ans;
}
pii Dfs(vvi &graph, int node, vi &visited) {
visited[node] = 1;
int ans = 1;
// int i = node % n, j = node / n;
// int edges = hor_edge[i][j] && hor_edge[i + 1][j] && ver_edge[i][j] && ver_edge[i][j + 1];
int edges = 2;
for (int neighbor : graph[node]) {
if (!visited[neighbor]) {
pii p = Dfs(graph, neighbor, visited);
ans += p.x;
edges += p.y;
} else if (visited[neighbor] == 1) {
edges--;
}
}
visited[node] = -1;
return {ans, edges};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
hor_edge.resize(n + 1, vb(m)), ver_edge.resize(n, vb(m + 1));
for (int i = 0; i <= n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
hor_edge[i][j] = c == '1';
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= m; j++) {
char c;
cin >> c;
ver_edge[i][j] = c == '1';
}
}
vvi graph(n * m);
vi visited(n * m);
// int ans = n * m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i < n - 1 && !hor_edge[i + 1][j]) {
graph[Val(i, j)].push_back(Val(i + 1, j));
graph[Val(i + 1, j)].push_back(Val(i, j));
}
if (j < m - 1 && !ver_edge[i][j + 1]) {
graph[Val(i, j)].push_back(Val(i, j + 1));
graph[Val(i, j + 1)].push_back(Val(i, j));
}
if (hor_edge[i][j] && hor_edge[i + 1][j] && ver_edge[i][j] && ver_edge[i][j + 1]) {
visited[Val(i, j)] = true;
}
}
}
// cout << Solve(true, 0, 0);
vii components;
for (int i = 0; i < n * m; i++) {
if (!visited[i]) components.push_back(Dfs(graph, i, visited));
}
if (components[0] > components[1]) swap(components[0], components[1]);
if (min(components[0].y, components[1].y) <= 3) {
cout << components[1].x - components[0].x;
} else {
cout << components[0].x - components[1].x;
}
// sort(all(components));
// int sa = 0, st = 0;
// for (int i = 0; i < components.size(); i++) {
// if (i & 1) {
// sa += components[i];
// } else {
// st += components[i];
// }
// }
// cout << sa - st;
// cout << -ans;
return 0;
}
//3 2
//11
//11
//01
//11
//000
//100
//100
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
224 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |