这是白皮书上的一道例题
----------
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 20;
const int INF = 2147483647;
int n,A[maxn][maxn],B[maxn][maxn];
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 r = 1; r < n ; r++ )
for(int c = 0; c < n ; c++)
{
int sum = 0;
if(r > 1)sum += B[r-2][c];
if(c > 0)sum += B[r-1][c-1];
if(c < n-1)sum += B[r-1][c+1];
B[r][c] = sum%2;
if(A[r][c]==1&&B[r][c]==0)return INF;
}
int cnt = 0;
for(int r = 0; r < n; r++ )
for(int c = 0 ; c < n ; c++)
if(A[r][c]!=B[r][c])cnt++;
return cnt;
}
int main()
{
int T;
scanf("%d",&T);
for(int kase = 1 ; kase <= T ; kase++ )
{
scanf("%d",&n);
for(int r0 = 0 ; r0 < n; r0++ )
for(int c0 = 0; c0 < n; c0++ )
scanf("%d",&A[r0][c0]);
int ans = INF;
for(int s = 0; s < (1<<n) ; s++ )
ans = min(ans,check(s));
if(ans==INF)ans=-1;
printf("Case %d: %d",kase,ans);
}
return 0;
}
UVA 11464 偶数矩阵
最新推荐文章于 2023-11-02 22:11:47 发布