codeforces——961C. Chessboard

本文是博主原创文章,未经允许不得转载。

我在csdn也同步发布了此文,链接 https://blog.csdn.net/umbrellalalalala/article/details/79892253

 

题目来源 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.

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×n正方形碎片,其中n为奇数,而且棋盘碎片上的方格颜色也不太对。现在我要将四个碎片拼成一个完整的棋盘,按照常理,棋盘的颜色是黑白相间的,但是由于碎片的颜色不太对,所以我需要改变一些碎片上的方格的颜色。问:我需要至少改变多少个方格的颜色,才能将碎片拼成一个完整的棋盘。样例输入就是四片碎片的形态,其中0代表白色方格,1代表个黑色方格。在拼接的时候只能平移碎片,不能旋转、反转。
 
【示例代码】(注释包含思路,注意理解其中0型棋盘和1型棋盘的含义,概念是我自定义的。我们最终只需要以最小的颜色改变数将四片棋盘变成两个0型棋盘和两个1型棋盘即可)
#include<stdio.h>  
#include<stdlib.h>  
#define MAX_N 105  
  
//分别用于输入四片碎片  
char chessboad1[MAX_N][MAX_N];  
char chessboad2[MAX_N][MAX_N];  
char chessboad3[MAX_N][MAX_N];  
char chessboad4[MAX_N][MAX_N];  
int main() {  
    int n;  
    int type[4][2];//0型棋盘是0比1多一位,1型棋盘则相反。type[i][0]存放若将第i片改变成0型需要改变几片的颜色,type[i][1]同理。最终我们需要将四片棋盘变成两个0型棋盘和两个1型棋盘  
    //The input model  
    scanf("%d", &n);  
    getchar();  
    for(int i=0;i<4;i++)  
        for (int j = 0; j < 2; j++) {  
            type[i][j] = 0;  
        }  
    for (int i = 0; i < n; i++) {  
        for (int j = 0; j < n; j++) {  
            scanf("%c", &chessboad1[i][j]);  
            if ((i + j) % 2 == 0) {  
                if (chessboad1[i][j] == '1')type[0][0]++;  
                else type[0][1]++;  
            }  
            else {  
                if (chessboad1[i][j] == '1')type[0][1]++;  
                else type[0][0]++;  
            }  
        }  
        getchar();  
    }  
    getchar();  
    for (int i = 0; i < n; i++) {  
        for (int j = 0; j < n; j++) {  
            scanf("%c", &chessboad2[i][j]);  
            if ((i + j) % 2 == 0) {  
                if (chessboad2[i][j] == '1')type[1][0]++;  
                else type[1][1]++;  
            }  
            else {  
                if (chessboad2[i][j] == '1')type[1][1]++;  
                else type[1][0]++;  
            }  
        }  
        getchar();  
    }  
    getchar();  
    for (int i = 0; i < n; i++) {  
        for (int j = 0; j < n; j++) {  
            scanf("%c", &chessboad3[i][j]);  
            if ((i + j) % 2 == 0) {  
                if (chessboad3[i][j] == '1')type[2][0]++;  
                else type[2][1]++;  
            }  
            else {  
                if (chessboad3[i][j] == '1')type[2][1]++;  
                else type[2][0]++;  
            }  
        }  
        getchar();  
    }  
    getchar();  
    for (int i = 0; i < n; i++) {  
        for (int j = 0; j < n; j++) {  
            scanf("%c", &chessboad4[i][j]);  
            if ((i + j) % 2 == 0) {  
                if (chessboad4[i][j] == '1')type[3][0]++;  
                else type[3][1]++;  
            }  
            else {  
                if (chessboad4[i][j] == '1')type[3][1]++;  
                else type[3][0]++;  
            }  
        }  
        getchar();  
    }  
    //The input model end  
      
    int temp;  
    for(int i=0;i<4-1;i++)  
        for (int j = 0; j < 4 - i - 1; j++) {  
            if (type[j][0] > type[j + 1][0]) {  
                temp = type[j][0];  
                type[j][0] = type[j + 1][0];  
                type[j + 1][0] = temp;  
                temp = type[j][1];  
                type[j][1] = type[j + 1][1];  
                type[j + 1][1] = temp;  
            }  
        }  
    printf("%d\n", type[0][0] + type[1][0] + type[2][1] + type[3][1]);  
    system("pause");  
    return 0;  
}  

 

posted @ 2018-04-13 22:00  umbrellalalalala  阅读(412)  评论(0编辑  收藏  举报