Even Parity 11464 (enumeration recurrence)

Source: Internet
Author: User

11464-even paritytime 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 ).
TheParityOf a cell is the number of 1 s surrounding that cell. A cell is surrounded by at most 4 cells (top, bottom, left, right ).

Suppose we have a grid of size 4x4:

 

1

0

1

0

The parity of each cell wocould 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 0 s to 1 s 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


Question: Give A 01 matrix of N * n (each element is not 0, that is, 1), select as few as possible 0 to 1, so that the upper and lower sides of each element are even. If no solution is available, output-1.

Analysis: the easiest way to think of is to enumerate each number and determine whether the entire Matrix meets the conditions. In this case, a maximum of 2 ^ 225 cases need to be enumerated, which is unacceptable.

Note that N is only 15, and each row must not exceed 2 ^ 15 = 32768, so we can enumerate the first row. Next we can calculate the second line based on the first line, and calculate the third line based on the second line ...... In this way, the total time complexity is reduced to 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 cannot be changed to 0} For (INT r = 1; r <n; r ++) {for (int c = 0; C <n; C ++) {int sum = 0; // The sum of the above, left, and right elements of Element B [r-1] [c] 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 cannot be changed to 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 ;}



Even Parity 11464 (enumeration recurrence)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.