POJ 2260 Error Correction

题目链接: POJ 2260

Describe:
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.
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.
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".
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
Sample Output:
OK
Change bit (2,3)
Corrupt

题目大意:

如果一个布尔矩阵,所有行上数字和,和所有列上数字和是偶数,则该矩阵具有奇偶性。给定一个布尔矩阵,让你来判断是否具有奇偶性,如果没有,能否改变一个数字使之具有奇偶性。

解题思路:

定义俩数组,一个是列和,一个是行和,输入同时把这俩数组也给记录上,然后看这俩数组中的数是奇是偶,如果是奇数,对应的奇数行cr++(或者奇数列cc++),如果最后cr=cc=0,则具有奇偶性,如果cr=cc=1,则改变对应的一个元素(i,j)即可使之具有奇偶性,其他情况则输出“Corrupt”

AC代码:

 

 1 #include 
 2 #include 
 3 #include 
 4 using namespace std;
 5 int main()
 6 {
 7     int n;
 8     int a[110][110]; // 矩阵存储
 9     int col[110],row[110]; // 行数组,列数组
10     while(~scanf("%d",&n) && n)
11     {
12         // 每一个样例前要先把数组清零
13         memset(a,0,sizeof(a));
14         memset(col,0,sizeof(col));
15         memset(row,0,sizeof(row));
16         // cc,cr初始化为0,x,y用来记录奇数行列
17         int cr = 0,cc = 0,x,y;
18         for(int i = 1; i <= n; i++)
19         {
20             for(int j = 1; j <= n; j++)
21             {
22                 scanf("%d",&a[i][j]);
23                 row[i] += a[i][j]; // 注意两个数组下标的不同
24                 col[j] += a[i][j];
25             }
26         }
27         for(int i = 1; i <= n; i++)
28         {
29             // 判断奇偶,不用%,用位运算,速度快
30             // & 1 奇数为1,偶数为0
31             if(col[i] & 1 == 1)
32             {
33                 cc++;
34                 y = i;
35             }
36             if(row[i] & 1 == 1)
37             {
38                 cr++;
39                 x = i;
40             }
41         }
42         // 判断,输出结果
43         if(cc == 0 && cr == 0) printf("OK\n");
44         else if(cc == 1 && cr == 1) printf("Change bit (%d,%d)\n",x,y);
45         else printf("Corrupt\n");
46     }
47     return 0;
48 }

 

小结:

矩阵用二维数组存储,取出修改元素复杂度为O(1)

 

你可能感兴趣的:(POJ 2260 Error Correction)