Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6820 | Accepted: 4286 |
Description
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
Output
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
Source
【題目大意】
給定一個n*n的由0/1構成的boolean矩陣,判斷是否各行各列和均爲偶數,若是輸出OK,若不是,可否更改其中一點的位置使行列均爲偶數。輸出這個點,若不可以,輸出Corrupt
【解題思路】
改一個點即可的充要條件爲行和列之中存在且僅有一行與一列的各自的和爲奇數。
或者利用鄰接矩陣的知識轉化爲有向圖+深度優先搜索??學姐講的圖論知識,還沒有深入學習。
【解題代碼】
#include <cstdio>
#include <cstring>
#define maxn 102
int map[maxn][maxn];
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
memset(map,0,sizeof(map));
int sumh=0,sumv=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
map[n+1][j]+=map[i][j];
map[i][n+1]+=map[i][j];
}
}
//solve it
int marka=0,markb=0,ans=0,bns=0;
for(int i=1;i<=n;i++)
{
if(map[n+1][i]%2)
{
bns++;
markb=i;
}
if(map[i][n+1]%2)
{
ans++;
marka=i;
}
}
if(!ans&&!bns) printf("OK\n");
else if(ans==1&&bns==1) printf("Change bit (%d,%d)\n",marka,markb);
else printf("Corrupt\n");
map[marka][markb]=!map[marka][markb];
}
}
【收穫與反思】
圖論知識補充: