Codeforces 961C Chessboard(将碎了的、染色乱了的棋盘碎片拼一起)
题目链接:点击打开链接
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 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 2nby 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
题目大意:问最少染多少色,可以将四块拼成一个完整的国际象棋棋盘
思路:四块碎片是无序的,随便以什么顺序拼,只要能完成一块棋盘就行
定义模式一:棋盘碎片的第一格为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(将碎了的、染色乱了的棋盘碎片拼一起)的更多相关文章
- codeforces——961C. Chessboard
本文是博主原创文章,未经允许不得转载. 我在csdn也同步发布了此文,链接 https://blog.csdn.net/umbrellalalalala/article/details/7989225 ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- Codeforces Gym100502A:Amanda Lounges(DFS染色)
http://codeforces.com/gym/100502/attachments 题意:有n个地点,m条边,每条边有一个边权,0代表两个顶点都染成白色,2代表两个顶点都染成黑色,1代表两个顶点 ...
- codeforces Gym 100286J Javanese Cryptoanalysis (二染色)
每一单词相邻两个字母,不能同时为元音或者辅音... 各种姿势都可以过:7个for,dp,黑白染色,dfs,并查集.... 最主要的思路就是相邻字母连边,把元音和辅音看成两个集合,那么有连边的两个字母一 ...
- codeforces#1217D. Coloring Edges(图上染色)
题目链接: https://codeforces.com/contest/1217/problem/D 题意: 给图染上$k$种颜色,相同颜色不能形成一个环 数据范围: $1\leq n \leq 5 ...
- Codeforces 383C Propagating tree, 线段树, 黑白染色思想
按深度染色,奇深度的点存反权值. #include <bits/stdc++.h> using namespace std; vector <]; ],a[],s[],vis[],i ...
- Codeforces 746D:Green and Black Tea(乱搞)
http://codeforces.com/contest/746/problem/D 题意:有n杯茶,a杯绿茶,b杯红茶,问怎么摆放才可以让不超过k杯茶连续摆放,如果不能就输出NO. 思路:首先,设 ...
- 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 ...
- Codeforces #254 div1 B. DZY Loves FFT 暴力乱搞
B. DZY Loves FFT 题目连接: http://codeforces.com/contest/444/problem/B Description DZY loves Fast Fourie ...
随机推荐
- Redis系列之----Redis的数据类型及使用场景
Redis是一个开源的.高性能的.基于键值对的缓存与存储系统,能够提供多种不同的键值数据类型来适应不同场景下的缓存和存储需求. Redis中所有的数据都存储在内存中,因此读写速度非常快,相 ...
- ios 页面滑动到底部无法往上滚的问题
简单说明:当h5端使用vue-infinite-scroll 插件 做滚动加载 如:页面布局 <header></header> <div ...
- Vuex实践(下)-mapState和mapGetters
Vuex系列文章 <Vuex实践(上)> <Vuex实践(中)-多module中的state.mutations.actions和getters> <Vuex实践(下)- ...
- 你知道Java中的CopyOnWriteArrayList吗?
CopyOnWrite CopyOnWrite是什么? CopyOnWriteArrayList源码分享? CopyOnWriteArrayList使用场景? CopyOnWriteArrayList ...
- cogs 397. [USACO Oct09] 热浪 Dijkstra
397. [USACO Oct09] 热浪 ★☆ 输入文件:heatwvx.in 输出文件:heatwvx.out 简单对比时间限制:1 s 内存限制:128 MB 德克薩斯純樸的民眾 ...
- HTTP图解笔记(六)—— 第6章 HTTP首部
前言 为啥第一章直接跳到第六章呢,因为...博主当初看书的时候挑着看..只看了第一章和第六章┗( ▔, ▔ )┛ HTTP图解对于不熟悉HTTP的小伙伴来说是很好的书籍,建议入手! 一. HTTP报文 ...
- 20200104模拟赛 问题A 图样
题目 分析: 老规矩,遇到期望要准备好随时投降... 大致想到了按位处理,然后分别下去搜索,再用组合数加加减减一下... 但是两个连通块之间连边的期望怎么算呢? 很好,投降... 下来看题解... 果 ...
- 定时任务莫名停止,Spring 定时任务存在 Bug??
Hello~各位读者新年好!这里楼下小黑哥给大家拜个年,祝大家蒸蒸日上烫烫烫,年年有余屯屯屯. 那年那 Bug 春节放假,小黑哥坐上高铁回家,突然想到一次生产问题.那是小黑哥参加工作第一年,那一年国庆 ...
- C#异常处理总结
Exception类分析 常见的异常类 异常捕获 异常处理原则和建议 SystemException类继承Exception,前者是System命名空间中所有其他异常类的基类,在捕获异常的时候,我首先 ...
- [ Python入门教程 ] Python中日期时间datetime模块使用实例
Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...