#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#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];
}
map<pair<vvb, vvb>, int> visited1;
map<pair<vvb, vvb>, int> visited2;
int Solve(bool player1) {
if (player1) {
auto it = visited1.find({hor_edge, ver_edge});
if (it != visited1.end()) {
return it->y;
}
} else {
auto it = visited2.find({hor_edge, ver_edge});
if (it != visited2.end()) {
return it->y;
}
}
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) s += b1 + b2;
// else s -= b1 + b2;
int val = Solve((b1 || b2) ? player1 : !player1);
if (player1) {
// s -= b1 + b2;
chkmax(ans, val + b1 + b2);
}
else {
// s += b1 + b2;
chkmin(ans, val - b1 - b2);
}
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) s += b1 + b2;
// else s -= b1 + b2;
int val = Solve((b1 || b2) ? player1 : !player1);
if (player1) {
// s -= b1 + b2;
chkmax(ans, val + b1 + b2);
}
else {
// s += b1 + b2;
chkmin(ans, val - b1 - b2);
}
ver_edge[i][j] = false;
}
}
}
if (ans == -infinity || ans == infinity) {
if (player1) {
visited1[{hor_edge, ver_edge}] = 0;
} else {
visited2[{hor_edge, ver_edge}] = 0;
}
return 0;
}
if (player1) {
visited1[{hor_edge, ver_edge}] = ans;
} else {
visited2[{hor_edge, ver_edge}] = ans;
}
return ans;
}
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';
}
}
cout << Solve(true);
return 0;
}
//3 2
//11
//11
//01
//11
//000
//100
//100
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Execution timed out |
1085 ms |
13404 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1063 ms |
6200 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
6 ms |
460 KB |
Output is correct |
3 |
Correct |
5 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
332 KB |
Output is correct |
5 |
Execution timed out |
1090 ms |
6496 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
9272 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Execution timed out |
1063 ms |
6200 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |