Chessboard
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.
Input
The 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.
Output
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
题意:有一个2*n X 2*n大小的棋盘,棋盘上的颜色有两种它们分别用1和0表示,它们的位置相互交错。现在有一个棋盘分裂为4个n*n大小的块,并且上面的颜色已经乱了,要求你把这些块拼在一起,要求求出使棋盘颜色符合要求所需改颜色的次数最少是多少。
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 1000000000
using namespace std;
typedef long long ll;
const int MAXN=1e9+10;
const int MAX=1e5+10;
const double eps=1e-6;
int n;
char s[5][110][110];
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
cin>>n;
for(int k=1;k<=4;k++)
for(int i=0;i<n;i++)
scanf("%s",&s[k][i]);
int ans=100000;
for(int k=1;k<=4;k++){
for(int h=k+1;h<=4;h++){
int y,x;
x=y=0;
for(int i=1;i<=4;i++)
if(i!=h&&i!=k){
if(!x) x=i;
else y=i;
}
int temp=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if((i%2==1&&j%2==1)||(i%2==0&&j%2==0)){
if(s[k][i][j]!='1') temp++;
if(s[h][i][j]!='1') temp++;
if(s[x][i][j]!='0') temp++;
if(s[y][i][j]!='0') temp++;
}
else{
if(s[k][i][j]!='0') temp++;
if(s[h][i][j]!='0') temp++;
if(s[x][i][j]!='1') temp++;
if(s[y][i][j]!='1') temp++;
}
}
}
ans=min(ans,temp);
}
}
cout<<ans<<endl;
return 0;
}