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


题意:给定n*n矩阵,可以把0变成1,求最少变幻次数使得每个位置的上下左右之和为偶数。

思路:n为15,第一行状态最多2^15种,然后由前一行可以推出后一行,如此一来,时间复杂度为O(2^n) * (n^2).

代码:

#include <stdio.h>
#include <string.h>
#define min(a,b) (a)<(b)?(a):(b)
#define INF 0x3f3f3f3f
const int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const int N = 15;
int t, n, map[N][N], save[N][N]; void init() {
scanf("%d", &n);
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++) {
scanf("%d", &map[i][j]);
}
} int Sum(int i, int j) {
int sum = 0;
for (int k = 0; k < 4; k ++) {
int xx = i + d[k][0];
int yy = j + d[k][1];
if (xx >= 0 && xx < n && yy >= 0 && yy < n) {
sum += save[xx][yy];
}
}
return sum;
} int cal(int state) {
int count = 0;
memset(save, 0, sizeof(save));
for (int i = 0; i < n; i ++)
for (int j = 0; j < n; j ++)
save[i][j] = map[i][j];
for (int i = n - 1; i >= 0; i --)
if (state >= (1<<i)) {
if (!save[0][n - 1 - i])
count ++;
save[0][n - 1 - i] = 1;
state -= (1<<i);
}
for (int i = 0; i < n - 1; i ++) {
for (int j = 0; j < n; j ++) {
if (Sum(i, j) % 2) {
if (save[i + 1][j])
return count = INF;
save[i + 1][j] = 1;
count ++;
}
}
}
for (int j = 0; j < n; j ++)
if (Sum(n - 1, j) % 2) {
count = INF;
break;
}
return count;
} int judge(int state) {
for (int i = 0; i < n; i ++)
if (map[0][i] == 1 && (state&(1<<i) == 0))
return false;
return true;
} void solve() {
int m = 1<<n, ans = INF;
for (int i = 0; i < m; i ++) {
if (judge(i)) {
ans = min(ans, cal(i));
}
}
if (ans == INF)
printf("-1\n");
else
printf("%d\n", ans);
} int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
init();
printf("Case %d: ", ++cas);
solve();
}
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(递归枚举)

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

  4. 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 ...

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

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

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

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

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

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

  8. UVa 11464 Even Parity 偶数矩阵

    给你一个 n * n 的 01 矩阵,现在你的任务是将这个矩阵中尽量少的 0 转化为 1 ,使得每个数的上下左右四个相邻的数加起来是偶数.求最少的转化个数. 首先,n 的规模并不大,最大只有15.但是 ...

  9. UVa 11464 - Even Parity

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

随机推荐

  1. nginx前端负载,后端apache获取真实IP设置

    原文链接: nginx前端负载,后端apache获取真实IP设置 参考文献: 前端Nginx,后端Apache获取用户真实IP地址  按照第二种方法设置不成功! 网站最前端是nginx,做的PROXY ...

  2. Ant学习实例

    ant   目录(?)[+] Ant学习实例 安装Ant 基础元素 project元素 target元素 property元素 完整示例   Ant学习实例 1.安装Ant 先从http://ant. ...

  3. 一起学习CMake – 03

    这一节我们就一起来看看如何用CMake来链接自己写的lib库,如何进行这些库文件的管理. 一个团队共同开发软件时,一般都是分模块进行作业的,每个人负责整个软件中的一部分,然后再整合成一个完整的软件系统 ...

  4. vc++窗口的创建过程(MFC消息机制的经典文章)

    一.什么是窗口类  在Windows中运行的程序,大多数都有一个或几个可以看得见的窗口,而在这些窗口被创建起来之前,操作系统怎么知道该怎样创建该窗口,以及用户操作该窗口的各种消息交给谁处理呢?所以VC ...

  5. 正则表达式验证数字、汉字、电话号码,email,整数,浮点数

    验证数字的正则表达式集 验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0- ...

  6. java web从零单排第二十二期《hibernate》代码分析之查看,删除用户信息

    前两期的内容不知道大家理解的怎么样,我并没有详细的去解释代码的意思,如果你已经自己都钻研明白了,那最好过,但还是一知半解的话,接下来我会仔细分析代码. 1.register.jsp:这部分代码只是简单 ...

  7. LeetCodeOJ. String to Integer (atoi)

    试题请參见: https://oj.leetcode.com/problems/string-to-integer-atoi/ 题目概述 Implement atoi to convert a str ...

  8. 演练5-6:Contoso大学校园管理系统6

    在上一次的教程中,我们处理了关联数据问题.这个教程演示如何处理并发问题.你将使用Department实体创建一个页面,这个页面在支持编辑和删除的同时,还可以处理并发错误.下面的截图演示了Index页面 ...

  9. C++静态库中使用_declspec(dllexport) 不能导出函数的问题

    在某项目中,有一些静态库,这些静态库中有类型命名的函数GET_XXX.在一次项目结构调整的时候,我想将调用这静态库的代码编译成DLL,并且将这些Get函数导出,我就直接就这些函数前面添加了_decls ...

  10. QThread 与 QObject的关系(QObject可以用于多线程,可以发送信号调用存在于其他线程的slot函数,但GUI类不可重入)

    QThread 继承 QObject..它可以发送started和finished信号,也提供了一些slot函数. QObject.可以用于多线程,可以发送信号调用存在于其他线程的slot函数,也可以 ...