Codeforces 961C 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 nn 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, j1 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.

Examples
Input
1
0

0

1

0
Output
1
Input
3
101
010
101

101
000
101

010
101
011

010
101
010
Output
2


题意:

给你四块分裂的边长为n正方形,每块正方形的颜色可以翻转,要把他拼成一块大的正方形,并且相邻的格子颜色要不同。求最少的反转次数

思路  :

1.首先要想到一点,只要知道了第一个数字,那么正方形中所有格子的数字就都知道了。

2.打表出两个边长为n的正方形,分别以0和1作为第一个数字。记为0号与1号

3.根据提议我们要把四个小正方形拼成一块大的,那么四块小正方形需要有两块和0号相同两块和1号。

4.一共有6种排列方法,枚举出来看哪种次数最小就完事了。

代码如下

#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
char chess[4][105][105];
int c1[105][105];
int c2[105][105];
void init(int a[4][2])
{
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<2;j++)
        {
            a[i][j]=0;
        }
    }
}
int main()
{
    int n;
    while((scanf("%d",&n))!=EOF)
    {

        for(int i=0;i<4;i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%s",chess[i][j]);
            }
        }
        c1[0][0]=0;
        c2[0][0]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i==0)
                {
                    c1[i][j]=1-c1[i][j-1];
                    c2[i][j]=1-c2[i][j-1];
                }
                else
                {
                    c1[i][j]=1-c1[i-1][j];
                    c2[i][j]=1-c2[i-1][j];
                }
            }
        }
        int num1[4][2];
        init(num1);
        for(int num=0;num<4;num++)
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    if ((chess[num][i][j]-'0')!=c1[i][j])
                    {num1[num][0]++;}
                    if ((chess[num][i][j]-'0')!=c2[i][j])
                    {num1[num][1]++;}
                }
            }
        }
        int ans1[5];
        int ans2[5];
        int k=0,l=0;
        for(int i=0;i<4;i++)
        {
            for(int j=i+1;j<4;j++)
            {
                ans1[k]=num1[i][0]+num1[j][0];
                k++;
            }
            for(int j=i+1;j<4;j++)
            {
               ans1[l]=num1[i][1]+num1[j][1];
                l++;
            }
        }
        int ans;
        ans=ans1[0]+ans2[5];
        for(int i=1;i<6;i++)
        {
            if(ans>ans1[i]+ans2[5-i])
            {
                ans=ans1[i]+ans2[5-i];
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/acm513828825/article/details/79856726