codeforces——961C. Chessboard
本文是博主原创文章,未经允许不得转载。
我在csdn也同步发布了此文,链接 https://blog.csdn.net/umbrellalalalala/article/details/79892253
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.
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.
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
1
0
0
1
0
1
3
101
010
101
101
000
101
010
101
011
010
101
010
2
#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; }