Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

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 
Case 2: 3 
Case 3: -1

 #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 模拟的更多相关文章

  1. UVA11464 Even Parity 搜索+递推

    问题描述 UVA11464 题解 第一直觉爆搜. 发现 \(N \le 15\) ,然后后面每行都可以通过第一行递推出来. 爆搜第一行,递推后面+check \(\mathrm{Code}\) #in ...

  2. Uva11464 Even Parity

    枚举每个格子的状态显然是不可能的. 思考发现,矩阵第一行的状态确定以后,下面的状态都可以递推出来. 于是状压枚举第一行的状态,递推全图的状态并判定是否可行. /*by SilverN*/ #inclu ...

  3. [UVA11464]Even Parity(状压,枚举)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. 【枚举暴力】【UVA11464】 Even Parity

    传送门 Description 给你一个0/1矩阵,可以将矩阵中的0变成1,问最少经过多少此操作使得矩阵任意一元素四周的元素和为偶数. Input 第一行是一个整数T代表数据组数,每组数据包含以下内容 ...

  5. 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵

    偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...

  6. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  7. ACM-ICPC 2018 沈阳赛区网络预赛 I. Lattice's basics in digital electronics 阅读题加模拟题

    题意:https://nanti.jisuanke.com/t/31450 题解:题目很长的模拟,有点uva的感觉 分成四步 part1 16进制转为二进制string 用bitset的to_stri ...

  8. 利用Teensy进行em410x卡模拟以及暴力破解em410x类门禁系统

    什么是低频?什么是EM410x? 首先,我不得不再次提一下那些工作在125khz频率下的低频卡(如:EM410X之类的),以便大家更好的阅读以下的内容. 什么是低频?以下就是低频的解释: 低频(LF, ...

  9. 沈阳网络赛I-Lattice's basics in digital electronics【模拟】

    42.93% 1000ms 131072K LATTICE is learning Digital Electronic Technology. He is talented, so he under ...

随机推荐

  1. 格式化JSON字符串

    提出需求 异步调用获取JSON数据时非常不直观,每次都需要格式化一次,才能直观的看到数据集合的结构,现在需要实现输出带缩进的格式. 实现效果 在浏览器的查看源文件中已经实现格式化,如果是页面使用,可以 ...

  2. HTML的基本标签

    整理一下这一周学习的一些知识. 首先是一些基本标签. <!DOCTYPE HTML><html> 文档类型声明: 让浏览器,按照html5的标准对代码进行解释与执行.文档类型声 ...

  3. 去掉vue地址栏中分隔#问题

    你需要开启HTML5 History 模式vue-router 默认 hash 模式 -- 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载.如果不想要很 ...

  4. jQuery === 面条式代码?

    自从React/Vue等框架流行之后,jQuery被打上了面条式代码的标签,甚至成了"过街老鼠",好像谁还在用jQuery,谁就还活在旧时代,很多人都争先恐后地拥抱新框架,各大博客 ...

  5. JavaScript中你所不知道的Object(二)--Function篇

    上一篇(JavaScript中你所不知道的Object(一))说到,Object对象有大量的内部属性,而其中多数和外部属性的操作有关.最后留了个悬念,就是Boolean.Date.Number.Str ...

  6. HTML解析原理概括(转载)

    HTML解析原理 标准的web前端工程师需要知道 ◎浏览器(或者相应播放器)的渲染/重绘原理  这我得加把劲了.我还真的说的不是很清楚,我就G下,结果不是很多,找到了有一个,就记下来了... 以下部分 ...

  7. 【java】关于java类和对象,你想知道的在这里!

    java类的基本组成 java作为一门面向对象的语言, 类和对象是最重要的概念之一,下面,就让我们来看看java中类的基本结构是怎样的: 一个简单的java类主要可由以下几个部分(要素)组成: 1.实 ...

  8. JS学习三(函数)

    [函数的声明格式] 1.函数的声明格式: function 函数名(参数1,参数2,...){ 函数体代码 return 返回值: } 函数的调用: ① 直接调用:函数名(参数1的值,参数2的值,.. ...

  9. 个人附加作业XD --这门课终于结束了~~

    你认为每次项目的评分标准存在哪些问题,你认为的合理评分准则是怎样的(个人/结对/团队算三个) 评分的话我个人觉得是存在一些问题的. 第一,分数差异 问题:一个就是各班的成绩评分有高有低,有的班整体分数 ...

  10. 【Alpha】——Seventh Scrum Meeting

    一.今日站立式会议照片 二.每个人的工作 成员 昨天已完成的工作 今天计划完成的工作 李永豪 将项目做成APK 用户界面改善 郑靖涛 协助设计账目一览表板块 用户界面改善 杨海亮 查询功能测试 用户界 ...