二进制枚举
#include <bits/stdc++.h>
using namespace std;
void turn(vector<int> &v, int i, int j) {
v[i] = v[i] ^ (1 << j);
if (i - 1 >= 0) v[i - 1] = v[i - 1] ^ (1 << j);
if (i + 1 < 5) v[i + 1] = v[i + 1] ^ (1 << j);
if (j - 1 >= 0) v[i] = v[i] ^ (1 << (j - 1));
if (j + 1 < 5) v[i] = v[i] ^ (1 << (j + 1));
}
int main() {
int tt;
cin >> tt;
while (tt--) {
int mn = 26;
vector<int> v;
for (int i = 0; i < 5; ++i) {
string s;
cin >> s;
bitset<5> b(s);
v.push_back(b.to_ulong());
}
for (int i = 0; i < (1 << 5); ++i) {
vector<int> tmp = v;
int res = 0;
for (int j = 0; j < 5; ++j) {
int t = (i >> j & 1);
if (t == 1) turn(tmp, 0, j), res++;
}
for (int j = 0; j < 4; ++j) {
if (tmp[j] != (1 << 5) - 1) {
for (int k = 0; k < 5; ++k) {
int t = (tmp[j] >> k & 1);
if (t == 0) turn(tmp, j + 1, k), res++;
}
}
}
if (tmp[4] == (1 << 5) - 1) mn = min(mn, res);
}
if (mn <= 6) cout << mn << endl;
else cout << -1 << endl;
}
return 0;
}