POJ 2260 Error Correction(水题)
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
题意:
给出一个只由0和1组成的n*n矩阵,如果它的每一行和每一列之和均是偶数,则我们称这个矩阵是“沃兹基扁德矩阵”,现在给你一个矩阵,请你判断它是否为“沃兹基扁德矩阵”,若不是,请判断是否存在一种方法,改变矩阵中的一个数字(0变1,1变0)使其变为“沃兹基扁德矩阵”。
题解:
水题,直接模拟就好了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int a[105][105];
int row[105],col[105];
int n;
bool check()
{
for(int i=0;i<n;i++)
if(row[i]%2||col[i]%2)
return false;
return true;
}
int main()
{
while(cin>>n&&n)
{
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
cin>>a[i][j];
row[i]+=a[i][j];
col[j]+=a[i][j];
}
if(check())
{
cout<<"OK"<<endl;
}
else
{
int ansi,ansj;
bool flag=false;
for(int i=0;i<n;i++)
{
if(row[i]%2)
{
for(int j=0;j<n;j++)
{
if(col[j]%2)
{
ansi=i,ansj=j;
row[i]--,col[j]--;
flag=true;
break;
}
}
if(flag)
break;
}
}
if(flag&&check())
printf("Change bit (%d,%d)\n",ansi+1,ansj+1);
else
cout<<"Corrupt"<<endl;
}
}
return 0;
}