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

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

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行,根据第二行又能计算出第三行...

代码如下:

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = ;
const int INF = ;
int n, A[maxn][maxn], B[maxn][maxn]; int check(int s) {
memset(B, , sizeof(B));
for(int c = ; c < n; c++) {
if(s & (<<c)) B[][c] = ;
else if(A[][c] == ) return INF; // 1不能变成0
}
for(int r = ; r < n; r++)
for(int c = ; c < n; c++) {
int sum = ; // 元素B[r-1][c]的上、左、右3个元素之和
if(r > ) sum += B[r-][c];
if(c > ) sum += B[r-][c-];
if(c < n-) sum += B[r-][c+];
B[r][c] = sum % ;
if(A[r][c] == && B[r][c] == ) return INF; // 1不能变成0
}
int cnt = ;
for(int r = ; r < n; r++)
for(int c = ; c < n; c++) if(A[r][c] != B[r][c]) cnt++;
return cnt;
} int main() {
int T;
scanf("%d", &T);
for(int kase = ; kase <= T; kase++) {
scanf("%d", &n);
for(int r = ; r < n; r++)
for(int c = ; c < n; c++) scanf("%d", &A[r][c]); int ans = INF;
for(int s = ; s < (<<n); s++)
ans = min(ans, check(s));
if(ans == INF) ans = -;
printf("Case %d: %d\n", kase, ans);
}
return ;
}

     

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. ionic如何uglify和minify你的js,css,image,png....

    Install:   1.ionic start myapp blank      2.cd myapp     3.npm install cordova-uglify or npm install ...

  2. hadoop-1.2.0安装记录

    一.添加用户(各机器均一致)     添加组: sudo addgroup hadoop     添加用户并到组:sudo adduser -ingroup hadoop hadoop 二.ssh无验 ...

  3. linux内核系列(二)内核数据结构之链表

    双向链表 传统链表与linu内核链表的区别图: 图一 图二 从上图中看出在传统链表中各种不同链表间没有通用性,因为各个数据域不同,而在linux内核中巧妙将链表结构内嵌到数据域结构中使得不同结构之间能 ...

  4. POJ-3207 Ikki's Story IV - Panda's Trick 2sat

    题目链接:http://poj.org/problem?id=3207 题意:在一个圆圈上有n个点,现在用线把点两两连接起来,线只能在圈外或者圈内,现给出m个限制,第 i 个点和第 j 个点必须链接在 ...

  5. 64位linux安装android sdk的问题

    截至到今天,似乎在64位机器下安装android sdk存在不能运行的问题,可以用以下方法解决: Android SDK requires: Fedora 17 64bit with 32bit An ...

  6. java 泛型中 T、E ... 和 问号(通配符)的区别

    一.泛型中T.E ...  是泛型类.泛型方法定义时候用的. 1.泛型类定义在类后面 紧跟类名后面 public class TestClassDefine<T>{} 2.泛型方法定义在方 ...

  7. sdk 命令行

    1.关闭服务命令:adb kill-server 2. 启动服务命令:adb start-server 3.查询当前设备命令:adb devices 4.安装应用程序命令:adb install [a ...

  8. 【转】C++中了类继承和调用父类的构造函数方法

    构造方法用来初始化类的对象,与父类的其它成员不同,它不能被子类继承(子类可以继承父类所有的成员变量和成员方法,但不继承父类的构造方法).因此,在创建子类对象时,为了初始化从父类继承来的数据成员,系统需 ...

  9. 【Servlet】Filter过滤器的编写和配置

    Servlet的Filter介绍 在Servlet作为过滤器使用时,它可以对客户的请求进行过滤处理,当它处理完成后,它会交给下一个过滤器处理,就这样,客户的请求在过滤链里一个个处理,直到请求发送到目标 ...

  10. BeagleBone Black Linux驱动程序开发入门(1): LED驱动程序

    这篇文章展示如何在BBB平台上编写LED驱动程序,本文的程序是根据国嵌S3C2440的LED驱动的例子并结合内核中OMAP系列的gpio操作来改的.本文中的程序包括驱动程序模块和用户空间程序.废话不多 ...