题目地址:http://vjudge.net/problem/UVA-11464
竟然是暴力,而不是找规律
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=(a);i<=(b);++i)
const int maxn=15+2;
const int INF=0x3f3f3f3f;
int A[maxn][maxn],B[maxn][maxn],n;
int Check(int s){memset(B,0,sizeof(B));REP(c,1,n) {if(s&(1<<c)) B[1][c]=1;else if(A[1][c]==1) return INF;}REP(r,2,n) REP(c,1,n){int sum=0;if(r>2) sum+=B[r-2][c];if(c>1) sum+=B[r-1][c-1];if(c<n) sum+=B[r-1][c+1];B[r][c]=sum&1;if(A[r][c]==1&&B[r][c]==0) return INF;}int cnt=0;REP(r,1,n) REP(c,1,n)if(B[r][c]!=A[r][c]) cnt++;return cnt;
}
int main(int argc, char const *argv[])
{int T,kase=0; scanf("%d",&T);while(T--){scanf("%d",&n);REP(r,1,n) REP(c,1,n) scanf("%d",&A[r][c]);int ans=INF;REP(s,1,(1<<(n+1))-1)ans=min(ans,Check(s));printf("Case %d: %d\n", ++kase,ans==INF?-1:ans);}return 0;
}