给定 n \times nn×n 由 00 和 11 组成的矩阵,如果矩阵的每一行和每一列的 11 的数量都是偶数,则认为符合条件。
你的任务就是检测矩阵是否符合条件,或者在仅改变一个矩阵元素的情况下能否符合条件。
“改变矩阵元素”的操作定义为 00 变成 11 或者 11 变成 00。
输入格式
输入 n + 1n+1 行,第 11 行为矩阵的大小 n(0 < n < 100)n(0<n<100),以下 nn 行为矩阵的每一行的元素,元素之间以一个空格分开。
输出格式
如果矩阵符合条件,则输出"OK"
;
如果矩阵仅改变一个矩阵元素就能符合条件,则输出需要改变的元素所在的行号和列号,以一个空格分开;
如果不符合以上两条,输出"Corrupt"
。
输出时每行末尾的多余空格,不影响答案正确性;
样例输入1
4
1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1
样例输出1
OK
样例输入2
4
1 0 1 0
0 0 1 0
1 1 1 1
0 1 0 1
样例输出2
2 3
样例输入3
4
1 0 1 0
0 1 1 0
1 1 1 1
0 1 0 1
样例输出3
Corrupt
思路:OK情况枚举每一行每一列的总和,注意总和赋值为0;
可以翻转的情况,找到需要翻转的行和列,输出即可;
不可以实现的情况,需要统计待翻转的情况cnt++,如果cnt>=2,说明不可以实现;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[100+10][100+10],n;
void solve(){
int ans=0;
int sum=0;
int c=0,r=0;
bool flag=false;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum+=a[i][j];
}
if(sum%2==0) sum=0;
else{
sum=0;
ans++;
c=i;
}
}
if(ans==0)
flag=true;
if(ans>=2){
cout<<"Corrupt"<<endl;
return;
}
sum=0,ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum+=a[j][i];
}
if(sum%2==0) sum=0;
else{
sum=0;
ans++;
r=i;
}
}
if(ans>=2){
cout<<"Corrupt"<<endl;
return;
}
if(flag&&!ans) cout<<"OK"<<endl;
else cout<<c+1<<" "<<r+1<<endl;
}
int main(){
while(~scanf("%d",&n)){
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&a[i][j]);
solve();
}
return 0;
}