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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 基于经纬度做航线图可视化的软件_碧蓝航线是哪家公司做的

    基于经纬度做航线图可视化的软件_碧蓝航线是哪家公司做的基于经纬度画航线图介绍代码介绍这阵子在处理航空公司的数据,为了PPT展示好看,做了几个可视化图。这里用的是pyecharts第三方库。pyecharts库的相关介绍,可以上设计文档看看相关说明。https://pyecharts.org/#/zh-cn/series_options代码importpandasaspddata=pd.read_csv(“airline_info.csv”,encoding=’gbk’)print(data)#数据太多,画出来太密了,这里选了

    2025年6月7日
    2
  • SQL Server 日期 字符串 格式转换 函数 datetime convert「建议收藏」

    SQL Server 日期 字符串 格式转换 函数 datetime convert「建议收藏」文章目录IntroSQLOthersIntro对某些表格数据进行查询时,常常有按照时间进行列值过滤的需求。SQLSQLServer内置函数CONVERT(data_type(length),data_to_be_converted,style)常见的两种转换需求:1.日期–>字符串2.字符串–>日期SQLselectgetdate(); –datetime–datetime–>stringdeclare@dateti

    2022年10月8日
    3
  • C语言switch语句用法_c语言switch语句格式

    C语言switch语句用法_c语言switch语句格式1、switch语句基本用法C语言中,switch语句是一种多分支选择语句,在实际应用中,要在多种情况中选择一种情况,执行某一部分语句。其使用一般形式如下:switch(表达式){case常量表达式1:语句块1;break;case常量表达式2:语句块2;break;……case常量表达式m:语句块m;break;default:语句块n;break;}使用说明如下:程序执行时,首先计算表达式的值,与case后面的常量表达式值比较,若相等就执行对应部分的语

    2022年8月30日
    1
  • 关于平面图到对偶图的转化

    关于平面图到对偶图的转化闲话哇对偶图真的是个好东西,昨天考NOI2010的时候前两道很快做完了,看着t3发呆了1个多小时,啥也想不出来.看着网格图突然想到听说bzoj1001狼抓兔子可以用对偶图求解.对偶图是啥我也不知道,听说把面看成点,连边后跑一边最短路就可以了.但是当时想到这个突然发现自己不会建对偶图,看时间还有一个多小时,于是建了8种可能的图,每一个都跑一遍spfa,发现有一个可以过样例,手

    2022年5月26日
    40
  • MinGW MinGW-w64 TDM-GCC等工具链之间的差别与联系「建议收藏」

    MinGW MinGW-w64 TDM-GCC等工具链之间的差别与联系

    2022年1月22日
    107
  • kafka使用场景举例_rabbitmq和kafka的区别面试

    kafka使用场景举例_rabbitmq和kafka的区别面试Kafka使用场景

    2022年10月15日
    3

发表回复

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

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