Codeforce961C(暴力)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值