链接:http://codeforces.com/problemset/problem/961/C
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
InputThe first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
OutputPrint one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
Examples1 0 0 1 0
1
3 101 010 101 101 000 101 010 101 011 010 101 010
2
题意:完整的棋盘被分成四部分。而且颜色也有些变化了(1是黑色,0是白色,原来1,0,是相邻的,相同的数不会在一起),让你用最小的代价重新合成这个棋盘(就是改变最少的颜色)。如果要组成完整的棋盘,必须有两个开头为0的,和两个开头为一的(n是奇数)。
#include<stdio.h>
#define inf 0x3f3f3f3f
#include<algorithm>
#include<string.h>
using namespace std;
int n;
char s1[200][200];
char s2[200][200];
char s3[200][200];
char s4[200][200];
char mp1[200][200]; //开头为0;
char mp2[200][200]; //开头为1;
int num[200][200];
int cmp(char a[200][200],char b[200][200])
{
int num=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
if(a[i][j]!=b[i][j])num++;
}
return num;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
scanf("%s",&s1[i]);
for(int i=0; i<n; i++)
scanf("%s",&s2[i]);
for(int i=0; i<n; i++)
scanf("%s",&s3[i]);
for(int i=0; i<n; i++)
scanf("%s",&s4[i]);
int ans=inf;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if((i+j)%2==0)
{
mp1[i][j]='0';
mp2[i][j]='1';
}
else
{
mp1[i][j]='1';
mp2[i][j]='0';
}
}
}
int t1=cmp(s1,mp1);
int t2=cmp(s1,mp2);
num[0][0]=t1;
num[0][1]=t2;
t1=cmp(s2,mp1);
t2=cmp(s2,mp2);
num[1][0]=t1;
num[1][1]=t2;
t1=cmp(s3,mp1);
t2=cmp(s3,mp2);
num[2][0]=t1;
num[2][1]=t2;
t1=cmp(s4,mp1);
t2=cmp(s4,mp2);
num[3][0]=t1;
num[3][1]=t2;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
for(int z=0;z<2;z++)
{
int a=0,b=0;
if(i==0)a++;
else b++;
if(j==0)a++;
else b++;
if(k==0)a++;
else b++;
if(z==0)a++;
else b++;
if(a==2&&b==2)
{
ans=min(ans,num[0][i]+num[1][j]+num[2][k]+num[3][z]);
}
}
printf("%d\n",ans);
}
return 0;
}