Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

 

D

Even Parity

Input: Standard Input

Output: Standard Output

 

We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a one(1). 
The parity of a cell is the number of 1s surrounding that cell. A cell is surrounded by at most 4 cells (top, bottom, left, right).

Suppose we have a grid of size 4 x 4:

1

0

1

0

The parity of each cell would be

1

3

1

2

1

1

1

1

2

3

3

1

0

1

0

0

2

1

2

1

0

0

0

0

0

1

0

0

For this problem, you have to change some of the 0s to 1s so that the parity of every cell becomes even. We are interested in the minimum number of transformations of 0 to 1 that is needed to achieve the desired requirement.

Input

The first line of input is an integer T (T<30) that indicates the number of test cases. Each case starts with a positive integer N(1≤N≤15). Each of the next N lines contain N integers (0/1) each. The integers are separated by a single space character.

Output

For each case, output the case number followed by the minimum number of transformations required. If it's impossible to achieve the desired result, then output -1 instead.

Sample Input                             Output for Sample Input

3 
3 
0 0 0 
0 0 0 
0 0 0 
3 
0 0 0 
1 0 0 
0 0 0 
3 
1 1 1 
1 1 1 
0 0 0 

 

Case 1: 0 
Case 2: 3 
Case 3: -1

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[][],n,b[][];
long long ab;
int fun1(int x,int y)
{
return b[x][y+]^b[x][y-]^b[x-][y];
}
int fun(int x)
{
if((ab&x)!=ab)return ;
memset(b,,sizeof(b));
int i,j,ans=;
for(i=n; i>=; i--)
{
b[][i]=x&;
if(b[][i]^a[][i])
{
if(b[][i])ans++;
}
x>>=;
}
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
{
b[i][j]=fun1(i-,j);
if(b[i][j]^a[i][j])
{
if(b[i][j])
ans++;
else return ;
}
}
}
return ans;
}
int main()
{
int T,i,j,k,size,ans;
scanf("%d",&T);
for(i=; i<=T; i++)
{
ans=;
memset(a,,sizeof(a));
scanf("%d",&n);
for(j=; j<=n; j++)
for(k=; k<=n; k++)
scanf("%d",&a[j][k]);
size=(<<n);
ab=;
for(j=; j<=n; j++)
{
ab=(ab<<)+a[][j];
}
for(j=; j<size; j++)
{
int y=fun(j);
ans=ans>y?y:ans;
}
if(ans==)ans=-;
printf("Case %d: %d\n",i,ans);
}
}

Even Parity uva11464 模拟的更多相关文章

  1. UVA11464 Even Parity 搜索+递推

    问题描述 UVA11464 题解 第一直觉爆搜. 发现 \(N \le 15\) ,然后后面每行都可以通过第一行递推出来. 爆搜第一行,递推后面+check \(\mathrm{Code}\) #in ...

  2. Uva11464 Even Parity

    枚举每个格子的状态显然是不可能的. 思考发现,矩阵第一行的状态确定以后,下面的状态都可以递推出来. 于是状压枚举第一行的状态,递推全图的状态并判定是否可行. /*by SilverN*/ #inclu ...

  3. [UVA11464]Even Parity(状压,枚举)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. 【枚举暴力】【UVA11464】 Even Parity

    传送门 Description 给你一个0/1矩阵,可以将矩阵中的0变成1,问最少经过多少此操作使得矩阵任意一元素四周的元素和为偶数. Input 第一行是一个整数T代表数据组数,每组数据包含以下内容 ...

  5. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  6. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  7. ACM-ICPC 2018 沈阳赛区网络预赛 I. Lattice's basics in digital electronics 阅读题加模拟题

    题意:https://nanti.jisuanke.com/t/31450 题解:题目很长的模拟,有点uva的感觉 分成四步 part1 16进制转为二进制string 用bitset的to_stri ...

  8. 利用Teensy进行em410x卡模拟以及暴力破解em410x类门禁系统

    什么是低频?什么是EM410x? 首先,我不得不再次提一下那些工作在125khz频率下的低频卡(如:EM410X之类的),以便大家更好的阅读以下的内容. 什么是低频?以下就是低频的解释: 低频(LF, ...

  9. 沈阳网络赛I-Lattice's basics in digital electronics【模拟】

    42.93% 1000ms 131072K LATTICE is learning Digital Electronic Technology. He is talented, so he under ...

随机推荐

  1. beanshell postprocessor解决编码

    beanshell postprocessor  String s=new String(prev.getResponseData(),"UTF-8");        char  ...

  2. snowflake主键生成策略

    1.snowflake简介 在分布式系统中,我们需要各种各样的ID,既然是ID那么必然是要保证全局唯一,除此之外,不同当业务还需要不同的特性,比如像并发巨大的业务要求ID生成效率高,吞吐大:比如某些银 ...

  3. 在Windows上使用Ubuntu共享的打印机

    Ubuntu下使用cups共享打印机, 是一种简单易用的方法.CUPS(Common UNIX Printing System,通用Unix打印系统)是Fedora Core3中支持的打印系统,它主要 ...

  4. oracle查看锁和释放锁

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt108 Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程 ...

  5. 标题:a++和++a的区别

    以前我也是老搞不懂a++和++a的区别, 后来看了很多资料, 终于总结出来一条规律, 小白专用! 看完这个例子就懂了: 例1:$a = 8, 求 ++a + a++ - --a + a-- + ++a ...

  6. 防止fixed元素遮挡其他元素的方法

    有多个页面,有的有固定的头部(设置了postion:fixed的元素),有的没有固定的头部,这时就有个问题,有固定头部的页面,头部会遮挡下面的内容,那怎么解决呢? <!DOCTYPE html& ...

  7. 【2017集美大学1412软工实践_助教博客】团队作业9——测试与发布(Beta版本)

    题目 团队作业9--测试与发布(Beta版本)(http://www.cnblogs.com/happyzm/p/6917253.html) 团队作业9-1 测试与发布成绩 分值 1 0.5 0.5 ...

  8. 201521123068《Java程序设计》第2周学习总结

    1.本周学习总结 认识了各种类型与变量的使用 学习运算符的基本使用 变量命名不可以使用数字和一些特殊字符(如*.&.%等)作为开头: 变量名称不可以与Java关键词同名 学习了类与对象的区别 ...

  9. 201521123060 《Java程序设计》第14周学习总结

    1.本周学习总结 1.1以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2.书面作业 1.MySQL数据库基本操作 1.1建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己 ...

  10. 201521123003《Java程序设计》第10周学习笔记

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...