12351 | 2024-12-12 20:46:24 | szil | Bináris mátrix | cpp17 | Accepted 100/100 | 574ms | 80444 KiB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 2001;
const int INF = 1e9;
int a[MAXN][MAXN], n, m;
vector<pair<int, int>> masks1[MAXN], masks2[MAXN];
int dp1[MAXN][2][2], dp2[MAXN][2][2];
void reset() {
for (int i = 1; i <= max(n, m); i++) {
masks1[i].clear();
masks2[i].clear();
}
}
bool is_ok(vector<pair<int, int>> &masks, int v1, int v2, int v3) {
for (auto [x, y] : masks) {
if ((v1 ^ v2) != x && (v2 ^ v3) != y) return false;
}
return true;
}
int calc_dp(int dp[MAXN][2][2], vector<pair<int, int>> masks[MAXN], int L) {
dp[1][0][1] = 1;
dp[1][1][1] = 1;
for (int i = 2; i <= L; i++) {
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
dp[i][a][b] = INF;
for (int c = 0; c < 2; c++) {
for (int d = 0; d < 2; d++) {
if (is_ok(masks[i-2], c, d, a) && is_ok(masks[i-1], d, a, b)) {
dp[i][a][b] = min(dp[i][a][b], dp[i-2][c][d]+a+b);
}
}
}
}
}
}
int res = INF;
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
res = min(res, dp[L][a][b]);
}
}
return res;
}
void solve() {
cin >> n >> m;
reset();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c; cin >> c;
a[i][j] = c == '1';
}
}
for (int i = 1; i <= n; i++) {
for (int j = 2; j < m; j++) {
masks1[j].emplace_back(a[i][j-1] == a[i][j], a[i][j] == a[i][j+1]);
}
}
for (int i = 2; i < n; i++) {
for (int j = 1; j <= m; j++) {
masks2[i].emplace_back(a[i-1][j] == a[i][j], a[i][j] == a[i+1][j]);
}
}
for (int i = 1; i <= max(n, m); i++) {
{
auto &v = masks1[i];
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
{
auto &v = masks2[i];
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
}
int ans1 = calc_dp(dp1, masks1, m);
int ans2 = calc_dp(dp2, masks2, n);
cout << (ans1+ans2>=INF ? -1 : ans1 + ans2) << "\n";
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while (t--) solve();
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 1ms | 320 KiB | ||||
subtask2 | 9/9 | ||||||
2 | Accepted | 1ms | 320 KiB | ||||
3 | Accepted | 1ms | 320 KiB | ||||
4 | Accepted | 1ms | 320 KiB | ||||
5 | Accepted | 1ms | 320 KiB | ||||
6 | Accepted | 1ms | 500 KiB | ||||
7 | Accepted | 1ms | 568 KiB | ||||
8 | Accepted | 1ms | 568 KiB | ||||
9 | Accepted | 1ms | 568 KiB | ||||
10 | Accepted | 1ms | 568 KiB | ||||
11 | Accepted | 1ms | 552 KiB | ||||
12 | Accepted | 1ms | 320 KiB | ||||
13 | Accepted | 1ms | 572 KiB | ||||
14 | Accepted | 1ms | 320 KiB | ||||
15 | Accepted | 1ms | 320 KiB | ||||
16 | Accepted | 1ms | 320 KiB | ||||
17 | Accepted | 1ms | 320 KiB | ||||
subtask3 | 12/12 | ||||||
18 | Accepted | 1ms | 320 KiB | ||||
19 | Accepted | 1ms | 568 KiB | ||||
20 | Accepted | 1ms | 568 KiB | ||||
21 | Accepted | 1ms | 320 KiB | ||||
22 | Accepted | 1ms | 320 KiB | ||||
23 | Accepted | 1ms | 320 KiB | ||||
24 | Accepted | 1ms | 320 KiB | ||||
25 | Accepted | 1ms | 500 KiB | ||||
subtask4 | 20/20 | ||||||
26 | Accepted | 1ms | 320 KiB | ||||
27 | Accepted | 1ms | 320 KiB | ||||
28 | Accepted | 1ms | 320 KiB | ||||
29 | Accepted | 1ms | 320 KiB | ||||
30 | Accepted | 1ms | 500 KiB | ||||
31 | Accepted | 1ms | 568 KiB | ||||
32 | Accepted | 1ms | 568 KiB | ||||
33 | Accepted | 1ms | 568 KiB | ||||
34 | Accepted | 1ms | 568 KiB | ||||
35 | Accepted | 1ms | 552 KiB | ||||
36 | Accepted | 1ms | 320 KiB | ||||
37 | Accepted | 1ms | 572 KiB | ||||
38 | Accepted | 1ms | 320 KiB | ||||
39 | Accepted | 1ms | 320 KiB | ||||
40 | Accepted | 1ms | 320 KiB | ||||
41 | Accepted | 1ms | 320 KiB | ||||
42 | Accepted | 1ms | 568 KiB | ||||
43 | Accepted | 1ms | 568 KiB | ||||
44 | Accepted | 4ms | 1064 KiB | ||||
45 | Accepted | 1ms | 568 KiB | ||||
46 | Accepted | 1ms | 568 KiB | ||||
47 | Accepted | 3ms | 824 KiB | ||||
48 | Accepted | 2ms | 568 KiB | ||||
49 | Accepted | 2ms | 572 KiB | ||||
50 | Accepted | 1ms | 568 KiB | ||||
51 | Accepted | 1ms | 320 KiB | ||||
52 | Accepted | 1ms | 508 KiB | ||||
53 | Accepted | 1ms | 564 KiB | ||||
subtask5 | 59/59 | ||||||
54 | Accepted | 1ms | 508 KiB | ||||
55 | Accepted | 1ms | 320 KiB | ||||
56 | Accepted | 1ms | 320 KiB | ||||
57 | Accepted | 1ms | 320 KiB | ||||
58 | Accepted | 1ms | 320 KiB | ||||
59 | Accepted | 1ms | 500 KiB | ||||
60 | Accepted | 1ms | 568 KiB | ||||
61 | Accepted | 1ms | 568 KiB | ||||
62 | Accepted | 1ms | 568 KiB | ||||
63 | Accepted | 1ms | 568 KiB | ||||
64 | Accepted | 1ms | 552 KiB | ||||
65 | Accepted | 1ms | 320 KiB | ||||
66 | Accepted | 1ms | 572 KiB | ||||
67 | Accepted | 1ms | 320 KiB | ||||
68 | Accepted | 1ms | 320 KiB | ||||
69 | Accepted | 1ms | 320 KiB | ||||
70 | Accepted | 1ms | 320 KiB | ||||
71 | Accepted | 1ms | 320 KiB | ||||
72 | Accepted | 1ms | 568 KiB | ||||
73 | Accepted | 1ms | 568 KiB | ||||
74 | Accepted | 1ms | 320 KiB | ||||
75 | Accepted | 1ms | 320 KiB | ||||
76 | Accepted | 1ms | 320 KiB | ||||
77 | Accepted | 1ms | 320 KiB | ||||
78 | Accepted | 1ms | 500 KiB | ||||
79 | Accepted | 1ms | 568 KiB | ||||
80 | Accepted | 1ms | 568 KiB | ||||
81 | Accepted | 4ms | 1064 KiB | ||||
82 | Accepted | 1ms | 568 KiB | ||||
83 | Accepted | 1ms | 568 KiB | ||||
84 | Accepted | 3ms | 824 KiB | ||||
85 | Accepted | 2ms | 568 KiB | ||||
86 | Accepted | 2ms | 572 KiB | ||||
87 | Accepted | 1ms | 568 KiB | ||||
88 | Accepted | 1ms | 320 KiB | ||||
89 | Accepted | 1ms | 508 KiB | ||||
90 | Accepted | 1ms | 564 KiB | ||||
91 | Accepted | 574ms | 80444 KiB | ||||
92 | Accepted | 321ms | 55704 KiB | ||||
93 | Accepted | 90ms | 13328 KiB | ||||
94 | Accepted | 54ms | 8956 KiB | ||||
95 | Accepted | 16ms | 2104 KiB | ||||
96 | Accepted | 8ms | 1336 KiB | ||||
97 | Accepted | 8ms | 1336 KiB | ||||
98 | Accepted | 6ms | 1080 KiB |