Even Parity UVA - 11464

问题

分析

枚举法,因为枚举全部的格子不现实,数量太多了,如果只枚举第一行,那么可以根据第一行推出第二行的情况,所以是可行的,时间复杂度 O ( n 2 ∗ 2 n ) O(n^2*2^n) O(n22n)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int maxn=15;
int mat[maxn][maxn],nmat[maxn][maxn],kase=0,T,n;

int main(void){
    scanf("%d",&T);
    while(kase<T) {
        scanf("%d",&n);
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                scanf("%d",&mat[i][j]);
            }
        }
        int s=(1<<n),ans=10000;
        for(int i=0;i<s;++i){
            memset(nmat,0,sizeof(nmat));
            int flag=true;
            for(int j=0;j<n;++j) {
                nmat[0][j]=(i&(1<<j))?1:0;
                if(nmat[0][j]==0 && mat[0][j]==1){
                    flag=false;
                    break;
                }
            }
            if(!flag) continue;
            for(int k=1;k<n;++k){
                for(int j=0;j<n;++j){
                    //计算nmat[k-1][j]的上面,左右三个的和
                    int tot=0;
                    if(k>1) tot+=nmat[k-2][j];
                    if(j>0) tot+=nmat[k-1][j-1];
                    if(j<n-1) tot+=nmat[k-1][j+1];
                    nmat[k][j]=(tot&1)?1:0;
                }
            }
            int t=0;
            for(int k=0;k<n;++k){
                for(int j=0;j<n;++j){
                    if(nmat[k][j]-mat[k][j]==1) ++t;
                    else if(nmat[k][j]-mat[k][j]==-1){
                        flag=false;
                        break;
                    }
                }
                if(!flag) break;
            }
            if(flag) ans=min(ans,t);
        }
        printf("Case %d: %d\n",++kase,(ans==10000)?-1:ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值