Error Correction
http://acm.zju.edu.cn/show_problem.php?pid=1949
Time limit: 1 Seconds
Memory limit: 32768K
A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of bits which are set. Here's a 4 x 4 matrix which has the parity property:
1 0 1 0 0 0 0 0 1 1 1 1 0 1 0 1
The sums of the rows are 2, 0, 4 and 2. The sums of the columns are 2, 2, 2 and 2.
Your job is to write a program that reads in a matrix and checks if it has the parity property. If not, your program should check if the parity property can be established by changing only one bit. If this is not possible either, the matrix should be classified as corrupt.
1. Input
The input will contain one or more test cases. The first line of each test case contains one integer n (n < 100), representing the size of the matrix. On the next n lines, there will be n integers per line. No other integers than 0 and 1 will occur in the matrix. Input will be terminated by a value of 0 for n.
2. Output
For each matrix in the input file, print one line. If the matrix already has the parity property, print "OK". If the parity property can be established by changing one bit, print "Change bit (i,j)" where i is the row and j the column of the bit to be changed. Otherwise, print "Corrupt".
3. Sample Input
4 1 0 1 0 0 0 0 0 1 1 1 1 0 1 0 1 4 1 0 1 0 0 0 1 0 1 1 1 1 0 1 0 1 4 1 0 1 0 0 1 1 0 1 1 1 1 0 1 0 1 0
4. Sample Output
OK Change bit (2,3) Corrupt
Problem Source: University of Ulm Local Contest 1998
1 //2007-01-11 21:26:33 Accepted 1949 C++ 00:00.01 404K
2 //writen by 曹高挺
3 #include <stdio.h>
4
5 bool mat[100][100];
6 bool row[100];
7 bool col[100];
8
9 int main(){
10 int size;
11 int i, j;
12 int posi, posj;
13 int oddi, oddj;
14 while( scanf("%d", &size), size ){
15 for(i=0; i<size; i++){
16 row[i] = col[i] = false;
17 }
18 for(i=0; i<size; i++){
19 for(j=0; j<size; j++){
20 scanf("%d", &mat[i][j]);
21 row[i] = row[i]^mat[i][j];
22 col[j] = col[j]^mat[i][j];
23 }
24 }
25 oddi = oddj = 0;
26 for(i=0; i<size; i++){
27 if( row[i] ){
28 oddi ++;
29 posi = i;
30 }
31 if( col[i] ){
32 oddj ++;
33 posj = i;
34 }
35 }
36 if( !oddi && !oddj ){
37 printf("OK\n");
38 continue;
39 }
40 if( oddi==1 && oddj==1 ){
41 printf("Change bit (%d,%d)\n", posi+1, posj+1);
42 continue;
43 }
44 printf("Corrupt\n");
45 }
46
47 return 1;
48 }