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 ...
随机推荐
- caffe cifar10试跑问题总结
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...
- bootstrap 基础(二)
1 栅格系统偏移 栅格系统的偏移只能向右:col-xs-offset-x. <!DOCTYPE html> <html lang="en"> <hea ...
- 使用SVG基本操作API
前面的话 本文将详细介绍SVG基本操作API,并使用这些API操作实例效果 基础API 在javascript中,可以使用一些基本的API来对SVG进行操作 [NS地址] 因为SVG定义在其自身的命令 ...
- JQuery中的表单验证及相关的内容
前 言 JRedu Android应用开发中,经常要用到表单.既然用到了表单,那就不可避免的要用到表单的验证.但是,在提交表单时,但是,并不是,每次提交的表单内容都是正确的,如果 每次都将表单的 ...
- 团队作业9——测试与发布(Beta版本)
Deadline: 2017-6-5 22:00PM,以博客发表日期为准 评分基准: 按时交 - 有分,检查的项目包括后文的两个方面 测试报告 发布说明 展示博客(单独一篇博客) 晚交 - 0分 迟交 ...
- Beta阶段事后诸葛亮分析
1.总结的提纲内容 a. 项目管理之事后诸葛亮会 设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要解决用户无意识花钱,无法清楚看见钱去 ...
- 201521123099 《Java程序设计》第八周学习总结
1. 本周学习总结 2. 书面作业 本次作业题集集合 List中指定元素的删除(题目4-1) 1.1 实验总结 老师上课解释的还有私下问同学大概能懂.主要理解到一点在删除List中元素时要考虑元素删除 ...
- 201521123038 《Java程序设计》 第四周学习总结
201521123038 <Java程序设计> 第四周学习总结 1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 1.通过 ins ...
- 201521123034《Java程序设计》第十周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题目4-2 1.1 截图你的提交结果(出 ...
- 201521123073 《Java程序设计》第14周学习总结
14周-数据库 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入. ...