Even Parity

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

  1. 3
  1. 3
  1. 0 0 0
  1. 0 0 0
  1. 0 0 0
  1. 3
  1. 0 0 0
  1. 1 0 0
  1. 0 0 0
  1. 3
  1. 1 1 1
  1. 1 1 1
  1. 0 0 0

Output for Sample Input

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

题目大意:给你一个n*n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上、下、左、右的元素(如果存在的话)之和均为偶数。

分析:偶数矩阵,关灯游戏改版。直接枚举会超时。注意到n只有15,第一行只有不超过2^15=32768中可能,所以第一行的情况可以枚举。接下来根据第一行可以完全计算出第2行,根据第二行又能计算出第三行...

代码如下:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. const int maxn = ;
  7. const int INF = ;
  8. int n, A[maxn][maxn], B[maxn][maxn];
  9.  
  10. int check(int s) {
  11. memset(B, , sizeof(B));
  12. for(int c = ; c < n; c++) {
  13. if(s & (<<c)) B[][c] = ;
  14. else if(A[][c] == ) return INF; // 1不能变成0
  15. }
  16. for(int r = ; r < n; r++)
  17. for(int c = ; c < n; c++) {
  18. int sum = ; // 元素B[r-1][c]的上、左、右3个元素之和
  19. if(r > ) sum += B[r-][c];
  20. if(c > ) sum += B[r-][c-];
  21. if(c < n-) sum += B[r-][c+];
  22. B[r][c] = sum % ;
  23. if(A[r][c] == && B[r][c] == ) return INF; // 1不能变成0
  24. }
  25. int cnt = ;
  26. for(int r = ; r < n; r++)
  27. for(int c = ; c < n; c++) if(A[r][c] != B[r][c]) cnt++;
  28. return cnt;
  29. }
  30.  
  31. int main() {
  32. int T;
  33. scanf("%d", &T);
  34. for(int kase = ; kase <= T; kase++) {
  35. scanf("%d", &n);
  36. for(int r = ; r < n; r++)
  37. for(int c = ; c < n; c++) scanf("%d", &A[r][c]);
  38.  
  39. int ans = INF;
  40. for(int s = ; s < (<<n); s++)
  41. ans = min(ans, check(s));
  42. if(ans == INF) ans = -;
  43. printf("Case %d: %d\n", kase, ans);
  44. }
  45. return ;
  46. }

     

UVA 11464 Even Parity(部分枚举 递推)的更多相关文章

  1. 状态压缩+枚举 UVA 11464 Even Parity

    题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...

  2. UVA.11464 Even Parity (思维题 开关问题)

    UVA.11464 Even Parity (思维题 开关问题) 题目大意 给出一个n*n的01方格,现在要求将其中的一些0转换为1,使得每个方格的上下左右格子的数字和为偶数(如果存在的话),求使得最 ...

  3. HRBUST 1211 火车上的人数【数论解方程/模拟之枚举+递推】

    火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从第3站起(包括第3站 ...

  4. UVA - 590Always on the run(递推)

    题目:UVA - 590Always on the run(递推) 题目大意:有一个小偷如今在计划着逃跑的路线,可是又想省机票费. 他刚開始在城市1,必须K天都在这N个城市里跑来跑去.最后一天达到城市 ...

  5. UVA 11464 Even Parity(递归枚举)

    11464 - Even Parity Time limit: 3.000 seconds We have a grid of size N x N. Each cell of the grid in ...

  6. 【UVA】11464 Even Parity(枚举子集)

    题目 传送门:QWQ 分析 标准的套路题. 枚举第一行,接着根据第一行递推下面的行. 时间复杂度$ O(2^n \times n^2) $ 代码 #include <bits/stdc++.h& ...

  7. hdu5965扫雷 枚举+递推

    题目链接 思路:枚举第一列的可能种数,然后递推即可,中途判断是否满足条件,最后再判断最后一列是否满足条件即可. #include<bits/stdc++.h> #define LL lon ...

  8. UVA 10943 - How do you add? 递推

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVa 926【简单dp,递推】

    UVa 926 题意:给定N*N的街道图和起始点,有些街道不能走,问从起点到终点有多少种走法. 很基础的dp.递推,但是有两个地方需要注意,在标记当前点某个方向不能走时,也要同时标记对应方向上的对应点 ...

  10. UVa 825【简单dp,递推】

    UVa 825 题意:给定一个网格图(街道图),其中有一些交叉路口点不能走.问从西北角走到东南角最短走法有多少种.(好像没看到给数据范围...) 简单的递推吧,当然也就是最简单的动归了.显然最短路长度 ...

随机推荐

  1. Java 并发之线程安全

    写线程安全的代码,说白了就是管理一个类的共享的.可变的状态.只要有多于 1 个线程对类的状态进行写入,那么就必须用同步来协调这多个线程对状态的访问.对于一个没有状态的类来说(简单的理解就是只有方法没有 ...

  2. about云开发虚拟化资源汇总,持续更新

    H3C实验手册H3C实验手册内容包括:1.帧中继典型配置举例一2.典型访问列表和地址转换综合应用配置案例3.交换机基本配置4.轮循DCC配置举例5.X.25典型配置举例6.MultiLink PPP配 ...

  3. HW4.44

    public class Solution { public static void main(String[] args) { double randX; double randY; int hit ...

  4. puppet_list

  5. (qsf文件 、 tcl文件 和 csv(txt)文件的区别) FPGA管脚分配文件保存、导入导出方法

    FPGA管脚分配文件保存方法 使用别人的工程时,有时找不到他的管脚文件,但可以把他已经绑定好的管脚保存下来,输出到文件里. 方法一: 查看引脚绑定情况,quartus -> assignment ...

  6. Web Service学习文旦下载

    Web Service的学习暂且告一段落,因为毕竟只是对它作简要了解,至于其原理什么并不打算涉及. 在这里我提供下我之前文档的整理版本: http://kuai.xunlei.com/d/YlzvAG ...

  7. Java开发中常见的危险信号(中)

    本文来源于我在InfoQ中文站原创的文章,原文地址是:http://www.infoq.com/cn/news/2013/12/common-red-flags-in-java-1 Dustin Ma ...

  8. c指针与数组,传参问题,指针数组与数组指针的区别,二维数组动态内存分配

    一 数组的结构:顺序存储,看谭浩强中的图,牢记 1.数组名指代一种数据结构:数组 现在可以解释为什么第1个程序第6行的输出为10的问题,根据结论1,数组名str的内涵为一种数据结构,即一个长度为10的 ...

  9. [Form Builder]NAME_IN()与COPY()

    NAME_IN和COPY实际是间接引用,类似指针传递,而不是值传递... IF :VAR1 IS NULL ...  direct referenceIF NAME_IN ( :VAR1 ) IS N ...

  10. Flume简介与使用(一)——Flume安装与配置

    Flume简介与使用(一)——Flume安装与配置 Flume简介 Flume是一个分布式的.可靠的.实用的服务——从不同的数据源高效的采集.整合.移动海量数据. 分布式:可以多台机器同时运行采集数据 ...