UVa 541 - Error Correction

来源:互联网 发布:查莉娅数据伤害 编辑:程序博客网 时间:2024/05/04 02:49

题目:在一个n x n的0和1组成的矩阵,问是否每行每列之和为偶数,如果不是是否通过改变一个元素值后成立。

分析:简单题。直接统计所有的行和列的偶数情况:

            1.都是偶数则成立;

            2.如果有1行和1列是奇数则改变交点的值即可;

            3.其他情况,均为不可能转换。

注意:数组开大一点。

#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;int mat[105][105];int main(){int n;while ( ~scanf("%d",&n) && n ) {for ( int i = 0 ; i < n ; ++ i )for ( int j = 0 ; j < n ; ++ j )scanf("%d",&mat[i][j]);int c_count = 0,c_save;int r_count = 0,r_save;for ( int i = 0 ; i < n ; ++ i ) {int count_c = 0,count_r = 0;for ( int j = 0 ; j < n ; ++ j ) {if ( mat[i][j]%2 ) count_c ++;if ( mat[j][i]%2 ) count_r ++;}if ( count_c%2 ) {c_count ++;c_save = i;}if ( count_r%2 ) {r_count ++;r_save = i;}}if ( r_count == 0 && c_count == 0 )printf("OK\n");else if ( r_count == 1 && c_count == 1 )printf("Change bit (%d,%d)\n",c_save+1,r_save+1);else printf("Corrupt\n");}return 0;}