Even Parity uva11464 模拟

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

[]   [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

 

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<time.h>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 int a[20][20],n,b[20][20];
 9 long long ab;
10 int fun1(int x,int y)
11 {
12     return b[x][y+1]^b[x][y-1]^b[x-1][y];
13 }
14 int fun(int x)
15 {
16     if((ab&x)!=ab)return 11111110;
17     memset(b,0,sizeof(b));
18     int i,j,ans=0;
19     for(i=n; i>=1; i--)
20     {
21         b[1][i]=x&1;
22         if(b[1][i]^a[1][i])
23         {
24             if(b[1][i])ans++;
25         }
26         x>>=1;
27     }
28     for(i=2; i<=n; i++)
29     {
30         for(j=1; j<=n; j++)
31         {
32             b[i][j]=fun1(i-1,j);
33             if(b[i][j]^a[i][j])
34             {
35                 if(b[i][j])
36                     ans++;
37                 else return 11111110;
38             }
39         }
40     }
41     return ans;
42 }
43 int main()
44 {
45     int T,i,j,k,size,ans;
46     scanf("%d",&T);
47     for(i=1; i<=T; i++)
48     {
49         ans=11111110;
50         memset(a,0,sizeof(a));
51         scanf("%d",&n);
52         for(j=1; j<=n; j++)
53             for(k=1; k<=n; k++)
54                 scanf("%d",&a[j][k]);
55         size=(1<<n);
56         ab=0;
57         for(j=1; j<=n; j++)
58         {
59             ab=(ab<<1)+a[1][j];
60         }
61         for(j=0; j<size; j++)
62         {
63             int y=fun(j);
64             ans=ans>y?y:ans;
65         }
66         if(ans==11111110)ans=-1;
67         printf("Case %d: %d\n",i,ans);
68     }
69 }
View Code

 

转载于:https://www.cnblogs.com/ERKE/p/3683810.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值