HUST OJ -1663

白成济
2023-12-01

F - A Special Property

Time Limit: 1s Memory Limit: 512MB

Submissions: 56 Solved: 23
DESCRIPTION

Given a boolean matrix, which means every element of it is either 0 or 1. If each row and each column of it has an even sum, we call it a magic matrix.

For example:

1 0 1 0

0 0 0 0

1 1 1 1

0 1 0 1

is a magic matrix.

If one magic is not a matrix, we want to know whether we can change only one bit to transform it into a magic matrix, or it should be treated as corrupt.

INPUT

The input file will contain more than one test cases.

The first line of each test case contains one integer n(n<100), which represents the size of the matrix.

The following n lines represents a boolean matrix.

OUTPUT

For each input, print one line.

Print "OK" if the given matrix is a magic matrix.

Print "Chang bit (i,j)" if change bit (i, j) can make it a magic matrix.

Print "Corrupt" if the given matrix is not OK and cannot be transform into a magic matrix with one bit changed.

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 1 1 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
0
SAMPLE OUTPUT
OK
Corrupt

Change bit (2,3)

水题HUST-OJ-1663

一开始看错题意,结果写出来是判定每行每列相等的了

开始敲之前一定要看清楚题意啊...多看几遍都没关系..不浪费时间..泪的教训

AC代码如下

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    int n;
    int i,j;
    int x1,x2;
    while(scanf("%d",&n)!=EOF)
    {
    	int flag1=0,flag2=0,flag3=0,flag4=0;
    	if(n==0) break;
        int a[n][n];
        int row[n],colum[n];
    	memset(row,0,n*sizeof(int));
    	memset(colum,0,n*sizeof(int));
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                row[i] += a[i][j];
        for(j=0;j<n;j++)
            for(i=0;i<n;i++)
                colum[j]+=a[i][j];
		for(i=0;i<n;i++)
			if(row[i]%2==1){
				if(flag1==1)
					flag3=1;
				else{
					flag1=1;
					x1 = i;
				}
			}
		for(i=0;i<n;i++)
			if(colum[i]%2==1){
				if(flag2==1)
					flag4=1;
				else{
					flag2=1;
					x2 = i;
				}
			}	

		if(flag3||flag4)
            printf("Corrupt\n");
        else if(flag1&&flag2)
        {
			printf("Change bit (%d,%d)\n",x1+1,x2+1);
        }
        else printf("OK\n");
    }

    return 0;
}

另...我把自己也不知道是否是完全正确的把这题条件改为各行列相等的代码也贴上来,留作纪念..
<pre name="code" class="plain">#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    int n;
    int i,j,b,c;
    int x1,x2;
    int tong[101];
    int flag1=0,flag2=0,flag3=0,flag4=0,flag5=0;
    memset(tong,0,101*sizeof(int));
    while(scanf("%d",&n)!=EOF)
    {
        int a[n][n];
        int row[n],colum[n];
    	memset(row,0,n*sizeof(int));
    	memset(colum,0,n*sizeof(int));
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                row[i] += a[i][j];
        for(j=0;j<n;j++)
            for(i=0;i<n;i++)
                colum[j]+=a[i][j];
        for(i=0;i<n;i++){
            tong[row[i]]++;
            tong[colum[i]]++;
        }
        for(i=1;i<101;i++){
            if (tong[i]==2){
                flag1=1;
                x1 = i;
            }
            if (tong[i]==2*n-2){
                flag2=1;
                x2 = i;
            }
            if(tong[i]==2*n)
                flag5 = 1;
        }
        if(flag1==1&&flag2==1)
        {
            for(i=0;i<n;i++)
            {
                if(row[i]==x1){
                    b = i;
                    flag3 = 1;
                }
                if(colum[i]==x1){
                    c = i;
                    flag4 = 1;
                }
            }
        }
        if(flag3&&flag4)
        {
            if((x1-x2)==1&&a[b][c]==1)
                printf("Change bit (%d,%d)\n",b+1,c+1);
            if((x1-x2)==-1&&a[b][c]==0)
                printf("Change bit (%d,%d)\n",b+1,c+1);
        }
        else if(flag5==1)
            printf("OK\n");
        else printf("Corrupt");
    }

    return 0;
}


 
 
 类似资料:

相关阅读

相关文章

相关问答