11464 - Even Parity

Time limit: 3.000 seconds

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

题意:给出一个n*n的01矩阵(每一个元素非0即1),选择尽量少的0变成1,使得每一个元素的上下左右的元素纸盒均为偶数。假设无解,输出-1.

分析:最easy想到的方法是枚举每一个数字“变”还是“不变”,最后推断整个矩阵是否满足条件。这样做最多须要枚举2^225种情况。难以承受。

注意到n仅仅有15,每一行仅仅有不超过2^15=32768种情况,所以能够枚举第一行的情况。

接下来能够依据第一行计算出第二行,依据第二行计算出第三行……这样,总时间复杂度降为O(2^n * n^2)。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 20;
const int INF = 0x3fffffff;
int n, A[N][N], B[N][N]; 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; //1不能变成0
}
for(int r = 1; r < n; r++) {
for(int c = 0; c < n; c++) {
int sum = 0; //元素B[r-1][c]的上、左、右3个元素之和
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(B[r][c] == 0 && A[r][c] == 1) return INF; //1不能变成0
}
}
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, cas = 0;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int r = 0; r < n; r++)
for(int c = 0; c < n; c++)
scanf("%d", &A[r][c]);
int ans = INF;
for(int i = 0; i < (1<<n); i++)
ans = min(ans, check(i));
if(ans == INF) ans = -1;
printf("Case %d: %d\n", ++cas, ans);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

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. UVA 11464 Even Parity(部分枚举 递推)

    Even Parity We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a on ...

  4. UVA 11464 - Even Parity(枚举方法)

    D Even Parity Input: Standard Input Output: Standard Output We have a grid of size N x N. Each cell ...

  5. UVa 11464 Even Parity (二进制法枚举)

    题意:给你一个n*n的01矩阵,让你把最少的0变成1,使得每个元素的上,下,左,右的元素(如果有的话)之和均为偶数. 析:最好想的的办法就是暴力,就是枚举每个数字是变还是不变,但是...时间复杂度也太 ...

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

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

  7. UVA - 11464 Even Parity 【暴力枚举】

    题意 给出一个 01 二维方阵 可以将里面的 0 改成1 但是 不能够 将 1 改成 0 然后这个方阵 会对应另外一个 方阵 另外一个方阵当中的元素 为 上 下 左 右 四个元素(如果存在)的和 要求 ...

  8. 【转载】UVa 11464 Even Parity 偶数矩阵

    题意:给你一个n*n的01矩阵,让你把这个矩阵中尽量少的0转换成1,使得矩阵每个位置的上下左右四个相邻的数加起来能被2整除,求最少的转换数 首先,n 的规模并不大,最大只有15.但是完全枚举整个矩阵显 ...

  9. UVa 11464 - Even Parity

    解题报告:题目大意有一个N×N的矩阵,矩阵中的元素只有1或0,如果说对于一个矩阵,它的所有的点的上下左右的点的和是偶数,则称这个矩阵为偶数矩阵,现在给你一个任意的矩阵,要求的是如果要把这个矩阵变成偶数 ...

随机推荐

  1. 《javascript高级编程》读书笔记(三)变量、范围和内存的问题

     第四章:变量.范围和内存的问题 检測类型:typeof是确定一个变量是字符串.数值.布尔值,还是undefined的最佳工具.可是假设变量的值是一个对象或null,typeof仅仅会返回" ...

  2. 关于mysql主从复制的概述与分类(转)

    一.概述: 按照MySQL的同步复制特点,大体上可以分为三种类别: 1.异步复制: 2.半同步复制: 3.完全同步的复制: -------------------------------------- ...

  3. 源代码分析:LayoutParams的wrap_content, match_parent, 而详细的价值观

    问题: 慢慢地熟悉android 的过程中.发现view 要么layout初始化,建或者生产活动是很清楚.被添加到父控制,然后开始了相应的生命周期.但父控件的整个界面.还是第一个系统view. 怎么来 ...

  4. JAVA程序生成XML标准化的文件格式,缩进,美化。

    //他开始Document映射到文件 TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer t ...

  5. 趣味Java算法题(附答案)

    [程序1]    题目:古典问题:有一对兔子,从出生后第3个月起每一个月都生一对兔子,小兔子长到第三个月后每一个月又生一对兔子,假如兔子都不死,问每一个月的兔子总数为多少?    //这是一个菲波拉契 ...

  6. 【原创】纯OO:从设计到编码写一个FlappyBird (六)

    第五部分请看这里 终于到了最后一个部分了! 这里使用SimpleJudge类来实现Judge接口. 首先是SimpleJudge需要的实例变量: 0.final LinkedList<Pilla ...

  7. Git相关操作汇总

    git clone: 正如上图,当我们打开终端的情况下,默认我们所在的目录是在/home/shiyanlou的,大家可以在终端输入以下命令把目录切换到桌面cd  /home/Desktop这个时候输入 ...

  8. 同一时候使用windows和linux系统

    相信非常多人又想使用方便的windows,可是在开发中必须使用linux,怎样选择呢? 没关系,这里教你怎样制作双系统. 下载wubi,仅仅有几兆大,直接在windows下安装,安装好以后,双系统就制 ...

  9. cocos2dx-3.0(13)------SpriteBatchNode与SpriteFrameCache渲染速度

    大家都知道一个游戏里面会有大量的图片,每一个图片渲染是须要时间的,以下分析两个类来加快渲染速度,加快游戏执行速度          一.SpriteBatchNode          1.先说下渲染 ...

  10. 得到JAVA项目根文件夹

    获得的相对路径 说明:相对路径(这并不说明什么时候相对谁)可以通过以下来获得(无论是一般java项目或web工程) String path = System.getProperty("use ...