/*********************
* Author:fisty
* Data:2014-10-29
* uva11464
* ******************/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX_N 20
const int INF = 1000000000;
int n, A[MAX_N][MAX_N],B[MAX_N][MAX_N];
int check(int s){
memset(B, 0, sizeof(B));
for(int j = 0;j < n; j++){
if(s & (1 << j)){
B[0][j] = 1;
}else if(A[0][j] == 1){
return INF;
}
}
for(int i = 1;i < n; i++){
for(int j = 0;j < n; j++){
int sum = 0;//计算B[i-1][j]的上左右三元素之和
if(i >= 2) sum += B[i-2][j];
if(j >= 1) sum += B[i-1][j-1];
if(j <= n-2) sum += B[i-1][j+1];
B[i][j] = sum % 2;
if(A[i][j] == 1 && B[i][j] == 0)
return INF;
}
}
int cnt = 0;
for(int i = 0;i < n; i++){
for(int j = 0;j < n; j++){
if(A[i][j] != B[i][j])
cnt++;
}
}
return cnt;
}
int main(){
//freopen("in","r", stdin);
int t;
scanf("%d", &t);
for(int cnt = 1; cnt <= t; cnt++){
scanf("%d", &n);
for(int i = 0;i < n; i++){
for(int j = 0;j < n; j++){
scanf("%d", &A[i][j]);
}
}
int ans = INF;
for(int s = 0;s < (1 << n); s++){
//枚举第一行1和0的所有可能
ans = min(check(s), ans);
}
if(ans == INF){
ans = -1;
}
printf("Case %d: %d\n", cnt, ans);
}
return 0;
}
uva11464Even Parity
最新推荐文章于 2015-03-22 16:24:25 发布