UVA 11464 Even Parity(递归枚举)

UVA 11464 Even Parity(递归枚举)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

11464 – Even Parity

Time limit: 3.000 seconds

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

题意:给出一个n*n的01矩阵(每一个元素非0即1),选择尽量少的0变成1,使得每一个元素的上下左右的元素纸盒均为偶数。假设无解,输出-1.

分析:最easy想到的方法是枚举每一个数字“变”还是“不变”,最后推断整个矩阵是否满足条件。这样做最多须要枚举2^225种情况。难以承受。

注意到n仅仅有15,每一行仅仅有不超过2^15=32768种情况,所以能够枚举第一行的情况。

接下来能够依据第一行计算出第二行,依据第二行计算出第三行……这样,总时间复杂度降为O(2^n * n^2)。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 20;
const int INF = 0x3fffffff;
int n, A[N][N], B[N][N];

int check(int s) {
    memset(B, 0, sizeof(B));
    for(int c = 0; c < n; c++) {
        if(s & (1<<c)) B[0][c] = 1;
        else if(A[0][c] == 1) return INF; //1不能变成0
    }
    for(int r = 1; r < n; r++) {
        for(int c = 0; c < n; c++) {
            int sum = 0; //元素B[r-1][c]的上、左、右3个元素之和
            if(r > 1) sum += B[r-2][c];
            if(c > 0) sum += B[r-1][c-1];
            if(c < n-1) sum += B[r-1][c+1];
            B[r][c] = sum % 2;
            if(B[r][c] == 0 && A[r][c] == 1) return INF; //1不能变成0
        }
    }
    int cnt = 0;
    for(int r = 0; r < n; r++)
        for(int c = 0; c < n; c++)
            if(A[r][c] != B[r][c])
                cnt++;
    return cnt;
}

int main() {
    int T, cas = 0;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        for(int r = 0; r < n; r++)
            for(int c = 0; c < n; c++)
                scanf("%d", &A[r][c]);
        int ans = INF;
        for(int i = 0; i < (1<<n); i++)
            ans = min(ans, check(i));
        if(ans == INF) ans = -1;
        printf("Case %d: %d\n", ++cas, ans);
    }
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117140.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql 误清空表 恢复数据

    mysql 误清空表 恢复数据把今天一不小心把订单表清空,怎么恢复数据的记录下来首先mysql需要开启bin-log,我的是宝塔面板,默认开启接下来进入到宝塔的www/server/data控制台输入find/-namemysqlbinlog-print找到binlog地址然后软连到usr/binln-fs/www/server/mysql/bin/mysqlbinlog/usr…

    2022年5月27日
  • 零拷贝java_java arraycopy方法

    零拷贝java_java arraycopy方法importcom.fasterxml.jackson.databind.JsonNode;//导入方法依赖的package包/类/***Appliesthisschemaruletotaketherequiredcodegenerationsteps.**AJava{@linkEnum}iscreated,withconstantsforeach…

    2022年10月2日
  • 成功的草根网站_草根网 真看不懂

    成功的草根网站_草根网 真看不懂 如果你是菜鸟站长,如果你可以耐心的看完这篇文章,那么你的网站建设道路会少一些磕拌,多一些平坦.以上两个如果若不满足可以直接回主版面了.  首先声明一下,我也是菜鸟,虽然接触网站制作很久了,从2000年开始的,但中途做做停停,耽误了太多的时间,所以到现在我也只是个菜鸟,充其量只是个老菜鸟吧.但正因为我也是菜鸟,所以我说的东西都是些浅显易懂的,应该会对各位新手有些帮助吧,至少是少走很多弯路!

    2022年10月18日
  • AC 自动机_模式匹配自动机

    AC 自动机_模式匹配自动机学习AC自动机的前提是要会trie数和KMP字符串匹配,它的功能是能对好多个模式串进行同时查找。比如对4个模式串:hehershisshe在一条母串中:shejjjjj查找每个模式串出现的次数.我们知道KMP算法有个next数组,和KMP类似,AC自动机有一个fail指针数组,用来对整棵trie树进行滚动。AC 自动机:HUD 3065:#i

    2022年10月29日
  • 解决lefse配置过程中遇到的问题「建议收藏」

    解决lefse配置过程中遇到的问题「建议收藏」记录centos7.5中配置lefse过程中遇到的问题1、下载lefse软件包,解压,查看“requirements.txt”文件,需要的R和python包列表:-R-Rlibraries:splines,stats4,survival,mvtnorm,modeltools,coin,MASS-pythonlibraries:rpy2(v.2.1orh…

    2022年5月13日
  • vue-router 详解

    vue-router 详解文章目录1、认识vue-router2、安装和使用vue-router1、认识vue-router目前前端流行的三大框架,都有自己的路由实现:Angular的ngRouterReact的ReactRouterVue的vue-routervue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用。我们可以访问其官方网站对其进行学习:https://router.vuejs.org/zh/vue-router是基于路由和组件的路由用户设定访问

    2022年7月11日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号