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怎样控制replace替换的次数?

    Mysql怎样控制replace替换的次数?

    2021年10月21日
    47
  • 基于ARM的嵌入式大容量数据存储解决方案「建议收藏」

    基于ARM的嵌入式大容量数据存储解决方案「建议收藏」恒颐成功案例   *  某单位舰载信息黑匣子;   *  消防主机监控与采集终端;应用背景  随着32位嵌入式微处理器的推广使用,越来越多的应用场合需要大容量的数据存储解决方案,传统的基于U盘、硬盘、SD/MMC卡存储方案,虽然也能实现大容量数据存储的功能,但无论是系统体积、成本、功耗、可靠性和易用性等方面都不尽如人意,因此,迫切需要一种能以较低的成本、功耗和体积,实现大容量、高

    2022年10月7日
    3
  • 解决win10状态栏的搜索框无法搜索本地应用或无反应

    解决win10状态栏的搜索框无法搜索本地应用或无反应今天突然出现的问题,在状态栏左下角的搜索框搜索OneNote没有任何反应,对,就是这个地方。最后在另一篇博客上找到了答案,那篇博客也是在知乎找到的答案,虽然是用被人的方法解决了问题,但我还是打算记下来;1、首先,打开管理员命令窗口,win+x,可以看到弹出一个窗口,打开windowsPowershell(管理员)如图2,输入下面这行英文startpowershell然…

    2022年6月4日
    37
  • 每个程序员都曾犯过的10大经典错误!

       作者 | Daan 策划 | 万佳 在程序员的职业生涯中,你都犯过哪些经典错误? 人非圣贤,孰能无过。对于犯错,你不用太困扰,因为对开发者而言,犯错太正常…

    2021年6月22日
    122
  • CSS3橙色的星球绕轨道公转动画

    效果:http://hovertree.com/texiao/css3/24/效果图:代码如下:转自:http://hovertree.com/h/bjaf/css3xingxi.htm特效汇总:

    2021年12月24日
    52
  • 广告平台精准推送系统解决方案架构「建议收藏」

    广告平台精准推送系统解决方案架构「建议收藏」以上就是广告精准推送的一个架构图。广告联盟是由多家广告提供商提供形成的一个组织,提供了多个平台的收集到的数据进行整合,数据的分析、清理,计算、统计等,提供向需要投放广告的广告主提供了一个投放系统平台。当用户进入门户网站或者app时,不同的用户看到的是不同的广告,广告联盟的系统计算出了不同用户或者用户群体的不同需求,通过广告推荐引擎系统和数据仓库中的统计数据以及用户的需求,展示给对应需求的用户观看,…

    2022年10月5日
    1

发表回复

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

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