123512024-12-12 20:46:24szilBináris mátrixcpp17Accepted 100/100574ms80444 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms320 KiB
subtask29/9
2Accepted1ms320 KiB
3Accepted1ms320 KiB
4Accepted1ms320 KiB
5Accepted1ms320 KiB
6Accepted1ms500 KiB
7Accepted1ms568 KiB
8Accepted1ms568 KiB
9Accepted1ms568 KiB
10Accepted1ms568 KiB
11Accepted1ms552 KiB
12Accepted1ms320 KiB
13Accepted1ms572 KiB
14Accepted1ms320 KiB
15Accepted1ms320 KiB
16Accepted1ms320 KiB
17Accepted1ms320 KiB
subtask312/12
18Accepted1ms320 KiB
19Accepted1ms568 KiB
20Accepted1ms568 KiB
21Accepted1ms320 KiB
22Accepted1ms320 KiB
23Accepted1ms320 KiB
24Accepted1ms320 KiB
25Accepted1ms500 KiB
subtask420/20
26Accepted1ms320 KiB
27Accepted1ms320 KiB
28Accepted1ms320 KiB
29Accepted1ms320 KiB
30Accepted1ms500 KiB
31Accepted1ms568 KiB
32Accepted1ms568 KiB
33Accepted1ms568 KiB
34Accepted1ms568 KiB
35Accepted1ms552 KiB
36Accepted1ms320 KiB
37Accepted1ms572 KiB
38Accepted1ms320 KiB
39Accepted1ms320 KiB
40Accepted1ms320 KiB
41Accepted1ms320 KiB
42Accepted1ms568 KiB
43Accepted1ms568 KiB
44Accepted4ms1064 KiB
45Accepted1ms568 KiB
46Accepted1ms568 KiB
47Accepted3ms824 KiB
48Accepted2ms568 KiB
49Accepted2ms572 KiB
50Accepted1ms568 KiB
51Accepted1ms320 KiB
52Accepted1ms508 KiB
53Accepted1ms564 KiB
subtask559/59
54Accepted1ms508 KiB
55Accepted1ms320 KiB
56Accepted1ms320 KiB
57Accepted1ms320 KiB
58Accepted1ms320 KiB
59Accepted1ms500 KiB
60Accepted1ms568 KiB
61Accepted1ms568 KiB
62Accepted1ms568 KiB
63Accepted1ms568 KiB
64Accepted1ms552 KiB
65Accepted1ms320 KiB
66Accepted1ms572 KiB
67Accepted1ms320 KiB
68Accepted1ms320 KiB
69Accepted1ms320 KiB
70Accepted1ms320 KiB
71Accepted1ms320 KiB
72Accepted1ms568 KiB
73Accepted1ms568 KiB
74Accepted1ms320 KiB
75Accepted1ms320 KiB
76Accepted1ms320 KiB
77Accepted1ms320 KiB
78Accepted1ms500 KiB
79Accepted1ms568 KiB
80Accepted1ms568 KiB
81Accepted4ms1064 KiB
82Accepted1ms568 KiB
83Accepted1ms568 KiB
84Accepted3ms824 KiB
85Accepted2ms568 KiB
86Accepted2ms572 KiB
87Accepted1ms568 KiB
88Accepted1ms320 KiB
89Accepted1ms508 KiB
90Accepted1ms564 KiB
91Accepted574ms80444 KiB
92Accepted321ms55704 KiB
93Accepted90ms13328 KiB
94Accepted54ms8956 KiB
95Accepted16ms2104 KiB
96Accepted8ms1336 KiB
97Accepted8ms1336 KiB
98Accepted6ms1080 KiB