Even Parity uva11464 模拟
| Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
|
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 |
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[][],n,b[][];
long long ab;
int fun1(int x,int y)
{
return b[x][y+]^b[x][y-]^b[x-][y];
}
int fun(int x)
{
if((ab&x)!=ab)return ;
memset(b,,sizeof(b));
int i,j,ans=;
for(i=n; i>=; i--)
{
b[][i]=x&;
if(b[][i]^a[][i])
{
if(b[][i])ans++;
}
x>>=;
}
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
{
b[i][j]=fun1(i-,j);
if(b[i][j]^a[i][j])
{
if(b[i][j])
ans++;
else return ;
}
}
}
return ans;
}
int main()
{
int T,i,j,k,size,ans;
scanf("%d",&T);
for(i=; i<=T; i++)
{
ans=;
memset(a,,sizeof(a));
scanf("%d",&n);
for(j=; j<=n; j++)
for(k=; k<=n; k++)
scanf("%d",&a[j][k]);
size=(<<n);
ab=;
for(j=; j<=n; j++)
{
ab=(ab<<)+a[][j];
}
for(j=; j<size; j++)
{
int y=fun(j);
ans=ans>y?y:ans;
}
if(ans==)ans=-;
printf("Case %d: %d\n",i,ans);
}
}
Even Parity uva11464 模拟的更多相关文章
- UVA11464 Even Parity 搜索+递推
问题描述 UVA11464 题解 第一直觉爆搜. 发现 \(N \le 15\) ,然后后面每行都可以通过第一行递推出来. 爆搜第一行,递推后面+check \(\mathrm{Code}\) #in ...
- Uva11464 Even Parity
枚举每个格子的状态显然是不可能的. 思考发现,矩阵第一行的状态确定以后,下面的状态都可以递推出来. 于是状压枚举第一行的状态,递推全图的状态并判定是否可行. /*by SilverN*/ #inclu ...
- [UVA11464]Even Parity(状压,枚举)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 【枚举暴力】【UVA11464】 Even Parity
传送门 Description 给你一个0/1矩阵,可以将矩阵中的0变成1,问最少经过多少此操作使得矩阵任意一元素四周的元素和为偶数. Input 第一行是一个整数T代表数据组数,每组数据包含以下内容 ...
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 I. Lattice's basics in digital electronics 阅读题加模拟题
题意:https://nanti.jisuanke.com/t/31450 题解:题目很长的模拟,有点uva的感觉 分成四步 part1 16进制转为二进制string 用bitset的to_stri ...
- 利用Teensy进行em410x卡模拟以及暴力破解em410x类门禁系统
什么是低频?什么是EM410x? 首先,我不得不再次提一下那些工作在125khz频率下的低频卡(如:EM410X之类的),以便大家更好的阅读以下的内容. 什么是低频?以下就是低频的解释: 低频(LF, ...
- 沈阳网络赛I-Lattice's basics in digital electronics【模拟】
42.93% 1000ms 131072K LATTICE is learning Digital Electronic Technology. He is talented, so he under ...
随机推荐
- MyBatis框架知识整理
MyBatis框架 一.介绍: MyBatis实际上是Ibatis3.0版本以后的持久化层框架[也就是和数据库打交道的框架]! 和数据库打交道的技术有: 原生的JDBC技术---> Spring ...
- MySQL DNS反查导致连接缓慢
场景 机器A上的一个模块连接机器B上的MySQL,在实验室网络环境下正常:同样A.B两台机器,网络环境切换为与外界隔离的一个小型局域网环境,A上的模块与B上MySQL建立连接非常慢. 环境 SuS ...
- hdu2222 Keywords Search(AC自动机初步)
题目大意: 给出多个模式串和一个主串,求多少个模式串在主串中出现过. 传送门 这是一道AC自动机的模板题. 在学习AC自动机之前,首先要学习WA自动机.TLE自动机和MLE自动机(雾 AC自动机是一种 ...
- JS中的phototype JS的三种方法(类方法、对象方法、原型方法)
JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...
- C++学习日记(二)————初始字符串类型
使用频率高,但操作复杂的数据有哪些? 做下总结: int; double;float;char;bool这些类型用的比较频繁,但并不复杂.但对于字符串来说(char数组)用的频繁但操作又复杂,只能用一 ...
- Java学习记录:降低耦合度
耦合度定义 耦合度(Coupling)是对模块间关联程度的度量.耦合的强弱取决与模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少. 模块间的耦合度是指模块之间的依赖关系,包括控制关系.调用 ...
- JPG、PNG和GIF图片的基本原理及优…
JPG.PNG和GIF图片的基本原理及优化方法 一提到图片,我们就不得不从位图开始说起,位图图像(bitmap),也称为点阵图像或绘制图像,是由称作像素(图片元素)的单个点组成的.这些点可以进行不同的 ...
- 交换机端口呈现err-disable的原因
导致交换机端口呈现err-disable状态的原因有很多,为方便大家查询,特归纳如下: 1. duplex mismatch (A is correct) 2. port-channel misc ...
- 【★】Web精彩实战之
JS精彩实战之<智能迷宫> ---宝贵编程经验分享会--- hello大家好,这里是Web云课堂,之前的一年里我们经历了Html和CSS的系统攻城,此时的你们已经是做静态(动静结 ...
- 201521123101 《Java程序设计》第5周学习总结
1. 本周学习总结 2. 书面作业 1. 代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误.并分析输出结果. 不能 ...