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,如果说对于一个矩阵,它的所有的点的上下左右的点的和是偶数,则称这个矩阵为偶数矩阵,现在给你一个任意的矩阵,要求的是如果要把这个矩阵变成偶数 ...
随机推荐
- hdu5115(区间dp)
n头狼排成一列,每头狼有两个属性,基础攻击力和附加攻击力, 第i只狼的基础攻击力是ai,附加攻击力是b(i-1) + b(i+1) 消灭一只狼,受到的伤害为基础攻击力+附加攻击力. 问消灭所有的狼受到 ...
- 移动web性能优化笔记
移动web性能优化 最近看了一些文章,对移动web性能优化方法,做一个简单笔记 笔记内容主要出自 移动H5前端性能优化指南和移动前端系列——移动页面性能优化
- Xamarin C# Android for Visual Studio 平台安装
原文:Xamarin C# Android for Visual Studio 平台安装 Xamarin是基于Mono的平台,目前主要有以下产品(更具体请见:http://xamarin.com/pr ...
- Drupal 7 电子邮件的发送设置 SMTP, Mail System, Mime Mail
尽管Drupal自带发送email功能,可是非常多server须要SMTP验证.这个时候就须要安装 SMTP 模块. 激活 SMTP 模块 进入配置 admin/config/system/smtp ...
- HBaseConvetorUtil 实体转换工具
HBaseConvetorUtil 实体转换工具类 public class HBaseConvetorUtil { /** * @Title: convetor * @De ...
- hdu3037(lucas定理)
给定n,m,p 表示<=m个豆子放在n棵树上,一共有多少种方案数, 总的方案书mod p 如果将m个豆子放在n棵树上, 可以使用插板法 得到方案数是C(n+m-1,n-1) 那么将0< ...
- 正确Linux新手很实用20命令
//正确Linux新手很实用20命令 //slwang 2014.4.19 1, ls list directory contents 内容 ls -l //以详情模式(long li ...
- IOS成长之路-Nsstring搜索方法rangeOfString
NSString *str1 = @"can you \n speak English"; NSString *str = @"\n"; //在str1该字符串 ...
- silverlight与wcf双向通讯 例子
本文将建立一个silverlight与wcf双向通讯的简单实例,以下是详细步骤: 新建Silverlight应用程序,名称WCFtest.解决方案中添加WCF服务应用程序,名称WcfServiceTe ...
- HBuilder之初体验
听闻HTML5定稿了,所以特意去了解了下.文章有提到HTML5的一款IDE(HBuilder,貌似出来好久了,孤陋寡闻....),于是来到官网http://dcloud.io/ ,被演示图震惊了!果然 ...