UVA 11464 Even Parity(递归枚举)
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 |
题意:给出一个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(递归枚举)的更多相关文章
- 状态压缩+枚举 UVA 11464 Even Parity
题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...
- UVA.11464 Even Parity (思维题 开关问题)
UVA.11464 Even Parity (思维题 开关问题) 题目大意 给出一个n*n的01方格,现在要求将其中的一些0转换为1,使得每个方格的上下左右格子的数字和为偶数(如果存在的话),求使得最 ...
- 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 ...
- UVA 11464 - Even Parity(枚举方法)
D Even Parity Input: Standard Input Output: Standard Output We have a grid of size N x N. Each cell ...
- UVa 11464 Even Parity (二进制法枚举)
题意:给你一个n*n的01矩阵,让你把最少的0变成1,使得每个元素的上,下,左,右的元素(如果有的话)之和均为偶数. 析:最好想的的办法就是暴力,就是枚举每个数字是变还是不变,但是...时间复杂度也太 ...
- 【UVA】11464 Even Parity(枚举子集)
题目 传送门:QWQ 分析 标准的套路题. 枚举第一行,接着根据第一行递推下面的行. 时间复杂度$ O(2^n \times n^2) $ 代码 #include <bits/stdc++.h& ...
- UVA - 11464 Even Parity 【暴力枚举】
题意 给出一个 01 二维方阵 可以将里面的 0 改成1 但是 不能够 将 1 改成 0 然后这个方阵 会对应另外一个 方阵 另外一个方阵当中的元素 为 上 下 左 右 四个元素(如果存在)的和 要求 ...
- 【转载】UVa 11464 Even Parity 偶数矩阵
题意:给你一个n*n的01矩阵,让你把这个矩阵中尽量少的0转换成1,使得矩阵每个位置的上下左右四个相邻的数加起来能被2整除,求最少的转换数 首先,n 的规模并不大,最大只有15.但是完全枚举整个矩阵显 ...
- UVa 11464 - Even Parity
解题报告:题目大意有一个N×N的矩阵,矩阵中的元素只有1或0,如果说对于一个矩阵,它的所有的点的上下左右的点的和是偶数,则称这个矩阵为偶数矩阵,现在给你一个任意的矩阵,要求的是如果要把这个矩阵变成偶数 ...
随机推荐
- Radmin远程控制软件
Radmin远程控制软件 日期:2015-08-20 作者:lujl Radmin是一款快速的远程控制软件,可以用来远程管理公司或个人计算机来实现远程办公.你可以通过鼠标和键盘来控制远程的 ...
- UltraEdit-32 温馨提示:右协会,取消 bak文件
1.最近安装UltraEdit-32 无权协会,能够 高级 ->组态 ->文件关联 在 检查 继承到资源管理器 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...
- MySQL Windows ZIP 免费安装和启动设置
MySQL Windows ZIP免安装版,设置和启动的过程事实上挺麻烦的.以下一步一步介绍使用的过程: 1.下载Windows (x86, 64-bit), ZIP Archive: 2.解压zip ...
- Mysql insert声明优化
1) 假设你同一时候从同一客户插入非常多行,使用多个值表的INSERT语句. 这比使用分开INSERT语句快(在一些情况中几倍). Insert into test values(1,2),(1 ...
- POJ 2738 Two Ends(记忆化)
Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...
- 修改系统启动项 grub2配置的方法 ubuntu[转]
在 早期的Ubuntu中,使用Grub作为系统的启动引导程序,想修改系统启动项非常简单,只要用gedit打开系统菜单设定文件( sudo gedit /boot/grub/menu.lst ),修改该 ...
- Oracle GoldenGate (以下简称ogg)在异种移植os同一种db之间的数据同步。
Oracle GoldenGate (以下简称ogg)在异种移植os同一种db之间的数据同步. ogg要实现的功能: 同步可以细化到单个表,满足特定的where条件rows同步,称号column同步. ...
- NET 项目结构搭建
NET 项目结构搭建 我们头开始,从简单的单项目解决方案,逐步添加业务逻辑的约束,从应用逻辑和领域逻辑两方面考虑,从简单的单个项目逐步搭建一个多项目的解决方案.主要内容:(1)搭建应用逻辑和领域逻辑都 ...
- 【转】Qt事件循环与线程 二
转自:http://blog.csdn.net/changsheng230/article/details/6153449 续上文:http://blog.csdn.net/changsheng230 ...
- WPF中的三维空间(1)
原文:WPF中的三维空间(1) WPF中可以创建三维几何图形,支持3D对象的应用,支持从3D Max等软件将3D文件obj导入设计中,但是目前还不支持将材质同时导入,这样需要在WPF中对3D对象重新设 ...