题目链接:点击打开链接

Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4pieces, 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, 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 2nby 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

Copy
1
0 0 1 0
output
1
input

Copy
3
101
010
101 101
000
101 010
101
011 010
101
010
output
2

题目大意:问最少染多少色,可以将四块拼成一个完整的国际象棋棋盘

思路:四块碎片是无序的,随便以什么顺序拼,只要能完成一块棋盘就行

定义模式一:棋盘碎片的第一格为1, 如样例二中的第一块碎片

定义模式二:棋盘碎片的第一格为0, 如样例二中的第四块碎片

显然一个完整的棋盘应该是由两块模式一、两块模式二组成的

于是我们只需要求出每块碎片变成模式一、变成模式二需要的染色数,然后暴力枚举哪两块碎片变成模式一(另外两块碎片变成模式二)总共需要的染色数最少,就行了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<set>
#include<queue>
#include<cmath>
typedef long long ll;
using namespace std;
const ll maxn = 1e6 + 10;
char a[105][105];
int m1[4], m2[4];
int main(){
int n;
memset(m1, 0, sizeof(m1));
memset(m2, 0, sizeof(m2));
scanf("%d",&n);
getchar();
for(int i = 0 ; i < 4 ; i++){
for(int j = 0; j < n ;j++){
scanf("%s", a[j]);
for(int k = 0; k < n; k++){
if((j+k)%2==0){
if(a[j][k]-'0' == 1) m2[i]++;
else if(a[j][k] - '0' == 0) m1[i]++;
}
else if((j+k)%2==1){
if(a[j][k]-'0' == 1)m1[i]++;
else if(a[j][k] - '0' == 0) m2[i]++;
}
}
}
}
int ans = n * n * 4;
for(int i = 0; i < 3; i++){//第i和j个棋盘碎片作为模式一,其他两个模式二
for(int j = i+1; j < 4; j++){
int sum = 0;
for(int k = 0; k < 4; k++){
if(k!=i && k!=j)sum+=m2[k];
}
sum+=(m1[i]+m1[j]);
//cout << sum << endl;
ans = min(ans, sum);
}
}
cout << ans << endl;
return 0;
}

Codeforces 961C Chessboard(将碎了的、染色乱了的棋盘碎片拼一起)的更多相关文章

  1. codeforces——961C. Chessboard

    本文是博主原创文章,未经允许不得转载. 我在csdn也同步发布了此文,链接 https://blog.csdn.net/umbrellalalalala/article/details/7989225 ...

  2. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  3. Codeforces Gym100502A:Amanda Lounges(DFS染色)

    http://codeforces.com/gym/100502/attachments 题意:有n个地点,m条边,每条边有一个边权,0代表两个顶点都染成白色,2代表两个顶点都染成黑色,1代表两个顶点 ...

  4. codeforces Gym 100286J Javanese Cryptoanalysis (二染色)

    每一单词相邻两个字母,不能同时为元音或者辅音... 各种姿势都可以过:7个for,dp,黑白染色,dfs,并查集.... 最主要的思路就是相邻字母连边,把元音和辅音看成两个集合,那么有连边的两个字母一 ...

  5. codeforces#1217D. Coloring Edges(图上染色)

    题目链接: https://codeforces.com/contest/1217/problem/D 题意: 给图染上$k$种颜色,相同颜色不能形成一个环 数据范围: $1\leq n \leq 5 ...

  6. Codeforces 383C Propagating tree, 线段树, 黑白染色思想

    按深度染色,奇深度的点存反权值. #include <bits/stdc++.h> using namespace std; vector <]; ],a[],s[],vis[],i ...

  7. Codeforces 746D:Green and Black Tea(乱搞)

    http://codeforces.com/contest/746/problem/D 题意:有n杯茶,a杯绿茶,b杯红茶,问怎么摆放才可以让不超过k杯茶连续摆放,如果不能就输出NO. 思路:首先,设 ...

  8. Codeforces Round #323 (Div. 2) D. Once Again... 乱搞+LIS

    D. Once Again... time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  9. Codeforces #254 div1 B. DZY Loves FFT 暴力乱搞

    B. DZY Loves FFT 题目连接: http://codeforces.com/contest/444/problem/B Description DZY loves Fast Fourie ...

随机推荐

  1. RabbitMQ安装(Windows)

    一.下载安装 由于RabbitMQ是用Erlang语言编写的,因此需要先安装Erlang. 通过http://www.erlang.org/downloads获取对应安装文件进行安装 增加环境变量ER ...

  2. 图解 kubernetes scheduler 架构设计系列-初步了解

    资源调度基础 scheudler是kubernetes中的核心组件,负责为用户声明的pod资源选择合适的node,同时保证集群资源的最大化利用,这里先介绍下资源调度系统设计里面的一些基础概念 基础任务 ...

  3. 2019 年 stackoverflow 网站最受欢迎的 20 个 Python 问题

    在最新一期的"Python开发者周刊"(Pycoder's weekly)里,我看到一则有意思的分享,故转出来分享给大家. 该分享来自是一份"python weekly ...

  4. 良心推荐,我珍藏的一些Chrome插件

    上次搬家的时候,发了一个朋友圈,附带的照片中不小心暴露了自己的 Chrome 浏览器插件之多,于是就有小伙伴评论说分享一下我觉得还不错的浏览器插件. 我下面就把我日常工作和学习中经常用到的一些 Chr ...

  5. KMP 和 扩展KMP

    KMP:在主串S中找子串T的位置KMP算法的时间复杂度O(|S|+|T|). #define maxn 1000 char s[maxn],t[maxn];//s为主串,t为子串 int net[ma ...

  6. Yolo V3损失函数占个坑

    https://blog.csdn.net/weixin_43384257/article/details/100974776目前来看讲的最清楚的博客 https://zhuanlan.zhihu.c ...

  7. Android学习进度一

    在解决了电脑产生的一系列问题之后成功安装了Android Studio,并在其自带的手机模拟器上成功运行了第一个App(Hello World!),通过这个最简单的App研究了App基本的工程结构,为 ...

  8. C++Primer第五版 6.1节练习

    练习6.1:实参和形参的区别是什么? 通俗解释: 实参是形参的初始值.编译器能以任意可行的顺序对实参求值.实参的类型必须与对应的形参类型匹配. 详解1) 形参变量只有在函数被调用时才会分配内存,调用结 ...

  9. eclipse反编译插件 jadclipse jad

    eclipse常用反编译插件jadclipse.jad 1.下载jadclipse:net.sf.jadclipse_3.3.0.jar,放到eclipse-plugins 2.下载jad.exe,放 ...

  10. Ansible playbooks常用模块案例操作

    打开git bash 连接ansible服务器,然后进入deploy用户 #ssh root@192.168.96.188 进入python3.6虚拟环境 #su - deploy #source . ...