UVA 11464 - Even Parity(枚举方法)
|
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 |
题意:给定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(枚举方法)的更多相关文章
- 状态压缩+枚举 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(递归枚举)
11464 - Even Parity Time limit: 3.000 seconds We have a grid of size N x N. Each cell of the grid in ...
- 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 (二进制法枚举)
题意:给你一个n*n的01矩阵,让你把最少的0变成1,使得每个元素的上,下,左,右的元素(如果有的话)之和均为偶数. 析:最好想的的办法就是暴力,就是枚举每个数字是变还是不变,但是...时间复杂度也太 ...
- 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 的 01 矩阵,现在你的任务是将这个矩阵中尽量少的 0 转化为 1 ,使得每个数的上下左右四个相邻的数加起来是偶数.求最少的转化个数. 首先,n 的规模并不大,最大只有15.但是 ...
- UVa 11464 - Even Parity
解题报告:题目大意有一个N×N的矩阵,矩阵中的元素只有1或0,如果说对于一个矩阵,它的所有的点的上下左右的点的和是偶数,则称这个矩阵为偶数矩阵,现在给你一个任意的矩阵,要求的是如果要把这个矩阵变成偶数 ...
随机推荐
- 转:DNS拾遗
最近帮朋友注册域名配置主机,碰到一些DNS上的一些概念,惭愧于有一些东西已经忘记是啥意思,于是决定重新学习一下DNS方面的基本概念. 常用概念: TTL: TTL为Time to live的缩写,网络 ...
- jquery ajax json 数据的遍历
需求:进行ajax请求后,后台传递回来以下json数据 { "data":[ {","name":"选择A","valu ...
- 转:CSS圆角详解
CSS3是样式表(style sheet)语言的最新版本,它的一大优点就是支持圆角. 网页设计大师Nicholas Zakas的最新文章,清晰易懂地解释了CSS3圆角的各个方面,非常值得学习.以下就是 ...
- C#委托好处知多少
1.性能 性能是泛型的一个主要优点. 直接上例子,通过实例可以让我们很好的理解这一点. Stopwatch stopwatch = new Stopwatch(); stopwatch.Start() ...
- 开源的Android视频播放器
之前尝试自己解码视频,然后播放显示,虽然音视频都可以播放,但是实现不了音视频的同步,所以使用第三方的视频库Vitamio来实现视频播放器功能,这样自己只需要实现播放解码的制作不不要关心底层解码和显示问 ...
- iOS开发中xib和Storyboard中需要注意的事项
使用xib注意事项: 1.只有自带view的控件才可以使用xib,因为它本身就是一个view 2.在使用可视化控件添加属性(代码)时候,如果删除了属性代码,一定要在xib上解除关联(不然会崩溃) 3. ...
- BZOJ AC 200题留念
话说本来想200AC就把题目总结一下...但是我现在挺懒的..不想弄...以后再来吧.
- CentOS6.5 服务器+apache5.3绑定多个域名+SELinux设置
下面简单的介绍了如何通过设置Apache的http.conf文件,进行多个域名以及其相关的二级域名的绑定(假设我们要绑定的域名是minidx.com和ntt.cc,二级域名是blog.minidx.c ...
- Python 第三篇(下):collections系列、集合(set)、单双队列、深浅copy、内置函数
一.collections系列: collections其实是python的标准库,也就是python的一个内置模块,因此使用之前导入一下collections模块即可,collections在py ...
- SuperSocket源码解析之开篇
一 简介 官方介绍:SuperSocket 是一个轻量级, 跨平台而且可扩展的 .Net/Mono Socket 服务器程序框架.你无须了解如何使用 Socket, 如何维护 Socket 连接和 S ...
