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 ...
随机推荐
- JavaScript 30 - 1 学习笔记
学习JavaScirpt30的笔记! ...虽然英语不是很好,但是跟着来还是学到了一些东西. 1-------> JavaScirpt Drum Kit 功能是这样的 ,敲击键盘上面的按钮, ...
- 关于request和response的中文乱码问题
相信大家在开发Web项目中都会遇到中文的请求乱码和响应乱码的情况,现在给大家梳理一下并提供解决方案. 1.为什么会出现乱码: 出现乱码的根本原因是浏览器和服务器的解码方式不一致引起的.所以我们统一编码 ...
- ElasticSearch和Kibana 5.X集群的安装
ElasticSearch和Kibana 5.X集群的安装 1.准备工作 1.1.下载安装包 1.2.系统的准备 2.ElasticSearch集群的安装 2.1.修改 config/elastics ...
- xcode模拟器不显示键盘解决方案
当我们使用Xcode进行开发的时候,并不是所有的时候都需要将代码运行在iPhone,有时候模拟器就可以解决这些问题, 但是当你使用模拟器的时候会发现,在TextFiled中输入信息时,如果你是用模拟器 ...
- 八大排序算法---基于python
本文节选自:http://python.jobbole.com/82270/ 本文用Python实现了插入排序.希尔排序.冒泡排序.快速排序.直接选择排序.堆排序.归并排序.基数排序. 1.插入排序 ...
- Spring中ApplicationContext加载机制
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp33 加载器目前有两种选择:ContextLoaderListener和Co ...
- Maven(一)初识Maven
前言 在这之前一直都有去看关于Maven的相关介绍,但是没有到真正要用的时候,自己总是以为懂了.其实真的感觉Maven并没有想象的那么简单! 那我们该怎么去学习maven呢?接下来我将从: 初步认识m ...
- JavaScript事件循环(Event Loop)机制
JavaScript 是单线程单并发语言 什么是单线程 主程序只有一个线程,即同一时间片断内其只能执行单个任务. 为什么选择单线程? JavaScript的主要用途是与用户互动,以及操作DOM.这决定 ...
- React Native底|顶部导航使用小技巧
导航一直是App开发中比较重要的一个组件,ReactNative提供了两种导航组件供我们使用,分别是:NavigatorIOS和Navigator,但是前者只能用于iOS平台,后者在ReactNati ...
- php的mysql语句里变量加不加单引号问题
第一种[{$_GET['id']}加不加单引号都能正常执行没问题] $sql = "select * from `news` where `id` = {$_GET['id']}" ...