Uva 11464 EvenParity
来源:互联网 发布:神作rpg 知乎 编辑:程序博客网 时间:2024/06/05 06:06
11464 - 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 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.
分析:最容易想到的方法是枚举每个数字“变”还是“不变”,最后判断整个矩阵是否满足条件。这样做最多需要枚举2^225种情况,难以承受。
注意到n只有15,每一行只有不超过2^15=32768种情况,所以可以枚举第一行的情况。接下来可以根据第一行计算出第二行,根据第二行计算出第三行……这样,总时间复杂度降为O(2^n * n^2)。
蓝书上的题 有点像开关灯问题
#include <cstdio>#define M 20int n, Min, a[M][M], b[M][M];int check(int x, int y)//将其上左右三面的值相加{ int sum = 0; if(x-1>=0) sum += b[x-1][y]; if(y-1>=0) sum += b[x][y-1]; if(y+1<n) sum += b[x][y+1]; return sum%2;//如果是偶数就返回0,奇数就返回1}void dfs(int cur){ //利用深度优先遍历枚举第一行 if(cur!=n) { b[0][cur] = 1; dfs(cur+1); b[0][cur] = 0; dfs(cur+1); } else//枚举完之后开始递推下面每一行的情况 { for(int i = 1; i < n; i++) for(int j = 0; j < n; j++) b[i][j] = check(i-1,j); int cou = 0; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(a[i][j]==1&&b[i][j]==0) return;//题目只能把0变1,不能把1变0,所以直接结束。 else if(a[i][j]==0&&b[i][j]==1) cou++;//只有当出现原来为0,枚举出的结果中为1的情况,cou才+1 if(Min>cou) Min = cou; return; }}int main (){ int cas, t = 0; scanf("%d",&cas); while(t++<cas) { scanf("%d",&n); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%d",&a[i][j]); Min = 1e9; dfs(0);//开始枚举; printf("Case %d: ",t); if(Min==1e9) printf("-1\n"); else printf("%d\n",Min); } return 0;}
- Uva 11464 EvenParity
- UVa 11464 EvenParity 偶数矩阵
- 【UVA】uva 11464
- uva 11464
- uva 11464
- uva 11464
- UVa 11464
- UVa 11464
- 训练指南uva 11464
- Uva 11464 偶检验
- UVA 11464 - Even Parity
- uva 11464 - Even Parity
- Uva-11464-Even Parity
- uva 11464Even Parity
- uva 11464 偶数矩阵
- Uva 11464 - Even Parity
- Uva 11464 - Even Parity
- uva 11464 - Even Parity
- 刷题笔记
- Hadoop 2.8 在 windows 平台上的搭建(单节点)
- O(n)的方法求最长回文子串长度(Manacher算法)
- 1013. 数素数 (20)
- [五]RabbitMQ-客户端源码之AMQChannel
- Uva 11464 EvenParity
- Windows10 64Bit完美配置Theano并实现GPU加速并解决((CNMeM is disable,CuDNN not available))
- CSS实现背景透明,文字不透明
- HTML 如何避免页面卡顿
- Ignite使用小记
- 随机数组不重复
- chapter5——打印杨辉三角形
- hibernate4
- bookStore第三篇【用户模块、购买模块、订单模块】