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. applicationContext.xml详解

    http://blog.csdn.net/heng_ji/article/details/7022171

  2. chrome 浏览器帐号登录不来,如何解决自己的书签

    装系统前把 该目录下  C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default    的  Bookmarks 复制出 ...

  3. MFC逆向-消息响应函数的定位

    MFC  ==   Microsoft Foundation Class,微软基础类库,他封装了Windows API以便用户更快速的开发界面功能程序然而该库及其庞大而复杂,需要有C++的功底否则很难 ...

  4. Linux内核启动及根文件系统载入过程

    上接博文<u-boot之u-boot-2009.11启动过程分析> Linux内核启动及文件系统载入过程 当u-boot開始运行bootcmd命令,就进入Linux内核启动阶段.与u-bo ...

  5. PS图片

    第二次练习PS,不知道找什么图片,就这么做着吧.然后自己学习了下动画制作,但是中间有些问题,需要再研究研究. 像 CS6中新建蒙版文档,画出的图形单位是厘米,怎么改为像素?(答案看后续博客....)

  6. C#如何在panl控件上添加Form窗体

    . if (treeView1.SelectedNode.Text == "个人信息") { Form1 f4 = new Form1(); f4.TopLevel = false ...

  7. 演练5-1:Contoso大学校园管理1

    **演练目的:掌握复杂模型的应用程序开发. Contoso大学校园管理系统功能包括学生.课程.教师的管理. 一.创建MVC Web应用程序 显示效果如下图,操作步骤略. 二.创建数据模型 1.创建学生 ...

  8. Android 去掉Activity的跳转动画

    startActivity或finish的时候调用一句话即可: overridePendingTransition(0, 0);

  9. perl 处理文本

    redis01:/root# cat abc GET /api/sale/get_voucher_list?loupan_id=32300&suid=kJIjl&loupan_site ...

  10. haroxy hdr

    ACL derivatives :ACL的衍生物 hdr([<name>[,<occ>]]) : exact string match 字符串精确匹配 hdr_beg([< ...