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

我在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[][];//0型棋盘是0比1多一位,1型棋盘则相反。type[i][0]存放若将第i片改变成0型需要改变几片的颜色,type[i][1]同理。最终我们需要将四片棋盘变成两个0型棋盘和两个1型棋盘
//The input model
scanf("%d", &n);
getchar();
for(int i=;i<;i++)
for (int j = ; j < ; j++) {
type[i][j] = ;
}
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
scanf("%c", &chessboad1[i][j]);
if ((i + j) % == ) {
if (chessboad1[i][j] == '')type[][]++;
else type[][]++;
}
else {
if (chessboad1[i][j] == '')type[][]++;
else type[][]++;
}
}
getchar();
}
getchar();
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
scanf("%c", &chessboad2[i][j]);
if ((i + j) % == ) {
if (chessboad2[i][j] == '')type[][]++;
else type[][]++;
}
else {
if (chessboad2[i][j] == '')type[][]++;
else type[][]++;
}
}
getchar();
}
getchar();
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
scanf("%c", &chessboad3[i][j]);
if ((i + j) % == ) {
if (chessboad3[i][j] == '')type[][]++;
else type[][]++;
}
else {
if (chessboad3[i][j] == '')type[][]++;
else type[][]++;
}
}
getchar();
}
getchar();
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
scanf("%c", &chessboad4[i][j]);
if ((i + j) % == ) {
if (chessboad4[i][j] == '')type[][]++;
else type[][]++;
}
else {
if (chessboad4[i][j] == '')type[][]++;
else type[][]++;
}
}
getchar();
}
//The input model end int temp;
for(int i=;i<-;i++)
for (int j = ; j < - i - ; j++) {
if (type[j][] > type[j + ][]) {
temp = type[j][];
type[j][] = type[j + ][];
type[j + ][] = temp;
temp = type[j][];
type[j][] = type[j + ][];
type[j + ][] = temp;
}
}
printf("%d\n", type[][] + type[][] + type[][] + type[][]);
system("pause");
return ;
}

codeforces——961C. Chessboard的更多相关文章

  1. Codeforces 961C Chessboard(将碎了的、染色乱了的棋盘碎片拼一起)

    题目链接:点击打开链接 Magnus decided to play a classic chess game. Though what he saw in his locker shocked hi ...

  2. Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs

    题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...

  3. Codeforces Round #254 (Div. 2):A. DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. CodeForces - 445A - DZY Loves Chessboard

    先上题目: A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input ...

  5. Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard(思维,构造)

    E. Connected Component on a Chessboard time limit per test2 seconds memory limit per test256 megabyt ...

  6. [题解]Codeforces Round #254 (Div. 2) A - DZY Loves Chessboard

    链接:http://codeforces.com/contest/445/problem/A 描述:一个n*m的棋盘,有一些格子不能放棋子.现在把黑白棋子往上放,要求放满且相邻格子的棋子颜色不同.输出 ...

  7. Codeforces Round #254 (Div. 2) A DZY Loves Chessboard

    先生成nXm的BW棋盘 BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB 类似上面交替变换 然后将输入为’-’的地方替换成‘-’即可 #include <iostream& ...

  8. Codeforces 445 A DZY Loves Chessboard【DFS】

    题意:给出n*m的棋盘,在‘.’处放上B或者W,最后要求所有的B和W都不相邻 先把棋盘的点转化成‘B’,再搜,如果它的四周存在‘B’,则将它变成'W' 一直挂在第五个数据的原因是,没有dfs(nx,n ...

  9. CodeForces - 445A - DZY Loves Chessboard解题报告

    对于这题本人刚开始的时候觉得应该用DFS来解决实现这个问题,但由于本人对于DFS并不是太熟,所以就放弃了这个想法: 但又想了想要按照这个要求实现问题则必须是黑白相间,然后把是字符是'B'或'W'改为' ...

随机推荐

  1. [C++学习历程]基础部分 C++中的类型和声明

    前面搭起了C++的VS环境,可以在VS中编写C++代码了,也运行了最简单的一个程序Helloworld.那么我们该怎么才能写出功能强大的程序,怎样才能随心所欲的应用呢,那就需要重新回头来,从C++基础 ...

  2. C语言符号优先级

    优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右   () 圆括号 (表达式)/函数名(形参表)   . 成员选择(对象) 对象.成员名   -& ...

  3. Lambda的使用与实战

    简介 (下面的简介也可以自己百度,一般进来的都是想知道怎么去用,所以这里主要也是重点在用法与实战上) Lambda表达式是Java SE 8中一个重要的新特性.lambda表达式允许你通过表达式来代替 ...

  4. 【Unity Shaders】Diffuse Shading——使用2D ramp texture来创建一个假的BRDF(双向反射分布函数)

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  5. Android重命名包名

    工程写的差不多了才发现原来用的包名还是自己尝试性的进行写代码的时候用到的.但apk的发布,google map api的申请等等方面都需要用到一个比较规范的包名.这就涉及到修改包名的问题. 包名一开始 ...

  6. Linux System Programming --Chapter Eight

    内存管理 一.分配动态内存的几个函数 用户空间内存分配:malloc.calloc.realloc1.malloc原型如下:extern void *malloc(unsigned int num_b ...

  7. 如何优化你的布局层级结构之RelativeLayout和LinearLayout及FrameLayout性能分析

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/51159419 如何优化你的布局层级结构之RelativeLayout和LinearLa ...

  8. Android Activity的四种经典传值方法

    文/ http://blog.csdn.net/sk719887916/article/details/41723613  skay 开发中遇到多个activity的传值问题 相邻两个之间的传值 或者 ...

  9. UTF-8是现在流行的编码方式,根据规定回答问题

    UTF-8是现在流行的编码方式,下面是RFC2279对UTF-8编码规则的规定 UCS-4 range (hex.) UTF-8 octet sequence (binary) 0000 0000-0 ...

  10. LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

    Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...