POJ 2260 Error Correction

9243 단어
제목 링크: 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:
41 0 1 00 0 0 01 1 1 10 1 0 141 0 1 00 0 1 01 1 1 10 1 0 141 0 1 00 1 1 01 1 1 10 1 0 10
Sample Output:
OKChange 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
"); 44 else if(cc == 1 && cr == 1) printf("Change bit (%d,%d)
",x,y); 45 else printf("Corrupt
"); 46 } 47 return 0; 48 }

 
소결:
행렬은 2차원 그룹으로 저장되며, 수정 요소를 추출하는 복잡도는 O(1)

좋은 웹페이지 즐겨찾기