-
Notifications
You must be signed in to change notification settings - Fork 0
/
11464.cpp
58 lines (49 loc) · 1.04 KB
/
11464.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 20;
const int INF = 1000000000;
int a[maxn][maxn], b[maxn][maxn];
int n;
int check(int s){
memset(b, 0, sizeof b);
for(int c = 0; c < n; c++){
if(s & (1<<c)) b[0][c] = 1;
else if(a[0][c] == 1) return INF;
}
for(int i = 1; i < n; i++){
for(int j = 0; j < n; j++){
int p = 0;
if(i-2>=0&&b[i-2][j]==1)p++;
if(j-1>=0&&b[i-1][j-1]==1)p++;
if(j+1<n&&b[i-1][j+1]==1)p++;
if(p%2==1) b[i][j] = 1;
else b[i][j] = 0;
}
}
int cnt = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(a[i][j] == 1&&b[i][j] == 0) return INF;
if(a[i][j] == 0&&b[i][j] == 1) cnt++;
}
}
return cnt;
}
int main(){
int kase = 0;
int t;
cin>>t;
while(t--){
cin>>n;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin>>a[i][j];
int ans = INF;
for(int s = 0; s < 1<<n; s++){
ans = min(ans, check(s));
}
if(ans == INF) ans = -1;
cout<<"Case "<<++kase<<": "<<ans<<endl;
}
}