zoukankan      html  css  js  c++  java
  • 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)
    ",ansi+1,ansj+1);
                else
                    cout<<"Corrupt"<<endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    [置顶] win7 && win8 下安装SqlServer 2008出现错误无法将对象实例化的问题
    [置顶] sizeof()和c++中变量们
    [置顶] sql2008 附加数据库 .mdf 出现错误 解决方案
    [置顶] 漂亮的 tab 样式
    [置顶] 认识指针和指针变量
    [置顶] SqlHelper类
    [置顶] 第一次使用事物 利用线性表
    [置顶] 设计模式之单例模式 (Design patterns of the The singleton pattern)c#
    [置顶] ListBox控件的数据绑定
    RedHat 6.0(64位)如何使用CentOS YUM源更新的方法
  • 原文地址:https://www.cnblogs.com/orion7/p/7930660.html
Copyright © 2011-2022 走看看