转自http://www.acmerblog.com/uva-11464-even-parity-4653.html

这道题的算法实现态变态了,奇妙的技巧,至少我是这样认为的。我想了很久也不会,书上代码看不懂,这篇讲的比较清晰。

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      

01 3
02 3
03 0 0 0
04 0 0 0
05 0 0 0
06 3
07 0 0 0
08 1 0 0
09 0 0 0
10 3
11 1 1 1
12 1 1 1
13 0 0 0

Output for Sample Input

1 Case 1: 0
2 Case 2: 3
3 Case 3: -1

解题报告:题目大意有一个N×N的矩阵,矩阵中的元素只有1或0,如果说对于一个矩阵,它的所有的点的上下左右的点的和是偶数,则称这个矩阵为偶数矩阵,现在给你一个任意的矩阵,要求的是如果要把这个矩阵变成偶数矩阵的话,最少需要将多少个点由1变成0,若不存在话,输出-1.(N<=15)

分析时间复杂度。 由题目可知矩阵的最大的行数为15,那么我们最容易想到就是去枚举每一个位置的数,那么矩阵的最多的元素为255,那么255个元素的状态为2^255,很明显这个是不可能实现的。
那么我们相到第一行的状态最多为2^15的,那么我们知道如果一个矩阵满足题目要求的话就是每一个元素的周围四个元素之和为偶数,那么我们就可以通过第一行求出第二行,利用第二行求出第三行…
那么我们只要去枚举第一行的2进制的每一个值,然后判断即可。

view source
01 #include<cstring> 
02 #include<cstdio> 
03 #include<iostream> 
04 #include<algorithm> 
05 using namespace std; 
06  
07 const int INF = 1<<30; 
08 const int MAXN = 20; 
09 int m[MAXN][MAXN]; 
10  
11 int solve(int n , int s){ 
12     int tmp[MAXN][MAXN]; 
13     memset(tmp , 0 , sizeof(tmp)); 
14     for(int i = 0 ; i < n ; i++){ 
15        if(s & (1<<i))//说明这一位是1 
16           tmp[0][i] = 1; 
17        else if(m[0][i] == 1)//这里得到的是0,但是实际上1不可能存在 
18           return INF; 
19     
20     //求出tmp数组 
21     for(int i = 1 ; i < n ; i++){ 
22        for(int j = 0 ; j < n ; j++){ 
23           int sum = 0;//通过前一行求出下一行 
24           if(i > 1)//加上 
25              sum += tmp[i-2][j]; 
26           if(j > 0)//加左 
27              sum += tmp[i-1][j-1]; 
28           if(j < n-1) 
29              sum += tmp[i-1][j+1]; 
30           //通过sum求出第tmp[i][j]; 
31           if(sum%2)//如果sum为奇数 
32              tmp[i][j] = 1; 
33           if(tmp[i][j] == 0 && m[i][j] == 1)//如果现在为0但是原先为1这种情况是不可能的 
34              return INF; 
35        
36     
37     //求出这次的变换的次数 
38     int cnt = 0; 
39     for(int i = 0 ; i < n ; i++) 
40        for(int j = 0 ; j < n ; j++) 
41           if(m[i][j] != tmp[i][j]) 
42              cnt++; 
43     return cnt; 
44
45  
46 int main(){  
47     int Case , n , cnt = 1; 
48     scanf("%d" , &Case); 
49     while(Case--){ 
50         scanf("%d" , &n); 
51         for(int i = 0 ; i < n ; i++) 
52            for(int j = 0 ; j < n ; j++) 
53               scanf("%d" , &m[i][j]); 
54         int ans = INF; 
55         for(int i = 0 ; i < (1<<n) ; i++)//枚举第一行的状态 
56            ans = min(ans , solve(n , i));//求最小的ans 
57         printf("Case %d: %d\n" , cnt++ , ans == INF ? -1 : ans); 
58     
59     return 0; 
60 }

   
UVA, 模拟
⇐ HDU 3054-Fibonacci[解题报告]HOJ
Uva 11538-Chess Queen[组合数学] ⇒

您可能还会对这些文章感兴趣!

  • HDU 3474-Necklace-模拟-[解题报告]HOJ
  • HDU 3451-Beat drop-BFS-[解题报告]HOJ
  • HDU 3432-Wax-模拟-[解题报告]HOJ
  • HDU 3413-Single CPU, multi-tasking-模拟-[解题报告]HOJ
  • HDU 3387-Calculator-模拟-[解题报告]HOJ
  • HDU 3386-Hauling Ore-模拟-[解题报告]HOJ
  • HDU 3378-San Guo Sha-模拟-[解题报告]HOJ
  • HDU 3347-Calculate the expression-模拟-[解题报告]HOJ
最新 最早 最热
  • 4条评论
  • 1条新浪微博
  • zhouleyu.com

    能不能说一下tmp数组和solve都是干嘛的?

    5月13日 回复 顶 转发

  • 我叫空格_
    1. 我叫空格_ 5月11日

      1楼

      能不能说一下tmp数组和solve都是干嘛的?       

    2. dragon 5月11日

      2楼

      遍历了所有的情况(2^n),tmp数组就是由当前遍历的状态s推算出的       

    懂了,谢谢啦

    5月13日 回复 顶 转发

  • dragon
    1. 我叫空格_ 5月11日

      1楼

      能不能说一下tmp数组和solve都是干嘛的?       

    遍历了所有的情况(2^n),tmp数组就是由当前遍历的状态s推算出的

    5月11日 回复 顶 转发

  • 我叫空格_

    能不能说一下tmp数组和solve都是干嘛的?

    5月11日 回复 顶 转发

社交帐号登录:

  • 微博
  • QQ
  • 人人
  • 豆瓣
  • 更多»

提交评论

返回顶部 |  站长统计 |  关于 |  登录  | 注册  |  网站地图  |  沪ICP备14003302号-1 | ©2014 ACM之家版权所有
Acm之家各大OJ题解 & 数据结构 & 算法 & IT企业面试题   搜索
  • 首页
  • ACM题库
  • 基础算法
  • 数据结构
  • 动态规划
  • 搜索
  • 图论
  • 数学相关
  • 计算几何
  • 专题系列
  • 剑指offer
  • 算法分类索引
首页 >  ACM题库 >  UVA > Uva-11464-Even Parity[模拟]
201402-27

Uva-11464-Even Parity[模拟]

coder UVA, 模拟法 围观113次 4条评论

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      

01 3
02 3
03 0 0 0
04 0 0 0
05 0 0 0
06 3
07 0 0 0
08 1 0 0
09 0 0 0
10 3
11 1 1 1
12 1 1 1
13 0 0 0

Output for Sample Input

1 Case 1: 0
2 Case 2: 3
3 Case 3: -1

解题报告:题目大意有一个N×N的矩阵,矩阵中的元素只有1或0,如果说对于一个矩阵,它的所有的点的上下左右的点的和是偶数,则称这个矩阵为偶数矩阵,现在给你一个任意的矩阵,要求的是如果要把这个矩阵变成偶数矩阵的话,最少需要将多少个点由1变成0,若不存在话,输出-1.(N<=15)

分析时间复杂度。 由题目可知矩阵的最大的行数为15,那么我们最容易想到就是去枚举每一个位置的数,那么矩阵的最多的元素为255,那么255个元素的状态为2^255,很明显这个是不可能实现的。那么我们相到第一行的状态最多为2^15的,那么我们知道如果一个矩阵满足题目要求的话就是每一个元素的周围四个元素之和为偶数,那么我们就可以通过第一行求出第二行,利用第二行求出第三行…那么我们只要去枚举第一行的2进制的每一个值,然后判断即可。

view source
01 #include<cstring> 
02 #include<cstdio> 
03 #include<iostream> 
04 #include<algorithm> 
05 using namespace std; 
06  
07 const int INF = 1<<30; 
08 const int MAXN = 20; 
09 int m[MAXN][MAXN]; 
10  
11 int solve(int n , int s){ 
12     int tmp[MAXN][MAXN]; 
13     memset(tmp , 0 , sizeof(tmp)); 
14     for(int i = 0 ; i < n ; i++){ 
15        if(s & (1<<i))//说明这一位是1 
16           tmp[0][i] = 1; 
17        else if(m[0][i] == 1)//这里得到的是0,但是实际上1不可能存在 
18           return INF; 
19     
20     //求出tmp数组 
21     for(int i = 1 ; i < n ; i++){ 
22        for(int j = 0 ; j < n ; j++){ 
23           int sum = 0;//通过前一行求出下一行 
24           if(i > 1)//加上 
25              sum += tmp[i-2][j]; 
26           if(j > 0)//加左 
27              sum += tmp[i-1][j-1]; 
28           if(j < n-1) 
29              sum += tmp[i-1][j+1]; 
30           //通过sum求出第tmp[i][j]; 
31           if(sum%2)//如果sum为奇数 
32              tmp[i][j] = 1; 
33           if(tmp[i][j] == 0 && m[i][j] == 1)//如果现在为0但是原先为1这种情况是不可能的 
34              return INF; 
35        
36     
37     //求出这次的变换的次数 
38     int cnt = 0; 
39     for(int i = 0 ; i < n ; i++) 
40        for(int j = 0 ; j < n ; j++) 
41           if(m[i][j] != tmp[i][j]) 
42              cnt++; 
43     return cnt; 
44
45  
46 int main(){  
47     int Case , n , cnt = 1; 
48     scanf("%d" , &Case); 
49     while(Case--){ 
50         scanf("%d" , &n); 
51         for(int i = 0 ; i < n ; i++) 
52            for(int j = 0 ; j < n ; j++) 
53               scanf("%d" , &m[i][j]); 
54         int ans = INF; 
55         for(int i = 0 ; i < (1<<n) ; i++)//枚举第一行的状态 
56            ans = min(ans , solve(n , i));//求最小的ans 
57         printf("Case %d: %d\n" , cnt++ , ans == INF ? -1 : ans); 
58     
59     return 0; 
60 }

   

UVA, 模拟
⇐ HDU 3054-Fibonacci[解题报告]HOJ
Uva 11538-Chess Queen[组合数学] ⇒

您可能还会对这些文章感兴趣!

  • HDU 3474-Necklace-模拟-[解题报告]HOJ
  • HDU 3451-Beat drop-BFS-[解题报告]HOJ
  • HDU 3432-Wax-模拟-[解题报告]HOJ
  • HDU 3413-Single CPU, multi-tasking-模拟-[解题报告]HOJ
  • HDU 3387-Calculator-模拟-[解题报告]HOJ
  • HDU 3386-Hauling Ore-模拟-[解题报告]HOJ
  • HDU 3378-San Guo Sha-模拟-[解题报告]HOJ
  • HDU 3347-Calculate the expression-模拟-[解题报告]HOJ
最新 最早 最热
  • 4条评论
  • 1条新浪微博
  • zhouleyu.com

    能不能说一下tmp数组和solve都是干嘛的?

    5月13日 回复 顶 转发

  • 我叫空格_
    1. 我叫空格_ 5月11日

      1楼

      能不能说一下tmp数组和solve都是干嘛的?       

    2. dragon 5月11日

      2楼

      遍历了所有的情况(2^n),tmp数组就是由当前遍历的状态s推算出的       

    懂了,谢谢啦

    5月13日 回复 顶 转发

  • dragon
    1. 我叫空格_ 5月11日

      1楼

      能不能说一下tmp数组和solve都是干嘛的?       

    遍历了所有的情况(2^n),tmp数组就是由当前遍历的状态s推算出的

    5月11日 回复 顶 转发

  • 我叫空格_

    能不能说一下tmp数组和solve都是干嘛的?

    5月11日 回复 顶 转发

社交帐号登录:

  • 微博
  • QQ
  • 人人
  • 豆瓣
  • 更多»

提交评论

返回顶部 |  站长统计 |  关于 |  登录  | 注册  |  网站地图  |  沪ICP备14003302号-1 | ©2014 ACM之家版权所有

even parity相关推荐

  1. 痞子衡嵌入式:常用的数据差错控制技术(2)- 奇偶校验(Parity Check)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是嵌入式里数据差错控制技术-奇偶校验. 在系列第一篇文章里,痞子衡给大家介绍了最简单的校验法-重复校验,该校验法实现简单,检错纠错能力都还不 ...

  2. DApp基础设施设计:借助Kubernetes、Docker和Parity实现可靠的以太坊事件跟踪

    本文最初发表于Hacker Noon博客,经原作者Carlo Las Marias授权由InfoQ中文站翻译分享. 学习Solidity并编写智能合约相对来讲是比较容易的,但是我们发现更加困难的技术挑 ...

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

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

  4. Leetcode PHP题解--D16 922. Sort Array By Parity II

    2019独角兽企业重金招聘Python工程师标准>>> 922. Sort Array By Parity II 题目链接 922. Sort Array By Parity II ...

  5. 用 Parity 发送 ERC20 Token

    Parity 是以太坊的 Rust 实现,它也内置了一个钱包.用过 Parity 的人都知道,用它发送 ETH 非常简单,点几下就行.但是可能不少人还不知道如何发送 ETH 以外的 ERC20 Tok ...

  6. Parity 錢包合約漏洞

    還記得今年 7 月 Parity 錢包合約被找到漏洞,結果駭客偷走了將近 150,000 個以太幣,會發生是因為智能合約的 callback 裡使用了 delegatecall(msg.data),這 ...

  7. Setting up Ethereum smart contract development using Parity on Ubuntu

    Ethereum represents one of the most interesting technological developments in the past few years, ta ...

  8. 风险平价策略python代码_风险平价组合(risk parity)理论与实践

    本文介绍了风险平价组合的理论与实践:后续文章将对risk parity组合进行更深入探讨以及引入预期收益后的资产配置实战策略. 前言 资产配置是个很广泛的话题,在投资中是一个非常重要的话题 从使用场景 ...

  9. Nearest Opposite Parity(反向建边+spfa)

    You are given an array aa consisting of nn integers. In one move, you can jump from the position ii ...

  10. Parity Game CodeForces - 298C

    You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears ...

最新文章

  1. FAIR发布两大更新:PyTorch1.8和一个10亿参数自监督模型,自监督也学GPT-3套路?
  2. 英特尔图形学专家被AMD挖走,研发实时光追技术,从部门主管变成副总裁
  3. CentOs基础操作指令(文件所属管理和权限管理)
  4. mysql cluster 子查询速度很慢
  5. 怎样用计算机制作思维导图,一篇文章告诉你如何绘制并运用思维导图!
  6. 理解全概率公式与贝叶斯公式
  7. 办公小技巧:excel列宽在哪里设置
  8. java题电影院售票设计报告_基于Java的电影订票网站的设计实现 任务书.doc
  9. PS CJ34预算转借
  10. 释放数据融合价值!腾讯Angel PowerFL荣获2021数博会“领先科技成果奖”
  11. VB实现移动鼠标产生粒子效果
  12. Linux---基础部分
  13. 腾讯云老用户重新注册新账号算新用户吗?
  14. python 期货交易接口_期货数据接口(期货数据接口 python)
  15. Android 集成Face++ 人脸识别(3.0+SDK)
  16. 使用mybatis注解实现模糊查询
  17. Vue全家桶系列之Vuex(一)
  18. 【Pytorch】常见的人脸身份识别损失函数
  19. FPGA基础知识----第二章 FPGA 开发流程
  20. matlab怎么求反馈增益矩阵,反馈增益矩阵状态反馈闭环传递函数矩阵为.PPT

热门文章

  1. 2014中兴笔试题 java_中兴2013Java开发笔试题目及答案.doc
  2. matlab贝叶斯回归,matlab使用贝叶斯优化的深度学习
  3. 一种新型双频双圆极化微带天线的设计
  4. 无监督学习之层次聚类算法
  5. 谷歌浏览器加载插件失败的解决方法
  6. 最全防雷器电路及保护电路解析
  7. SQL Server—查询表结构
  8. MacBook上可以玩的游戏「分手厨房」
  9. 无胁科技-TVD每日漏洞情报-2022-10-24
  10. 多彩标题文字PR字幕模板PR项目工程文件