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[][];//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的更多相关文章
- Codeforces 961C Chessboard(将碎了的、染色乱了的棋盘碎片拼一起)
题目链接:点击打开链接 Magnus decided to play a classic chess game. Though what he saw in his locker shocked hi ...
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs
题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...
- 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 ...
- CodeForces - 445A - DZY Loves Chessboard
先上题目: A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input ...
- 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 ...
- [题解]Codeforces Round #254 (Div. 2) A - DZY Loves Chessboard
链接:http://codeforces.com/contest/445/problem/A 描述:一个n*m的棋盘,有一些格子不能放棋子.现在把黑白棋子往上放,要求放满且相邻格子的棋子颜色不同.输出 ...
- Codeforces Round #254 (Div. 2) A DZY Loves Chessboard
先生成nXm的BW棋盘 BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB 类似上面交替变换 然后将输入为’-’的地方替换成‘-’即可 #include <iostream& ...
- Codeforces 445 A DZY Loves Chessboard【DFS】
题意:给出n*m的棋盘,在‘.’处放上B或者W,最后要求所有的B和W都不相邻 先把棋盘的点转化成‘B’,再搜,如果它的四周存在‘B’,则将它变成'W' 一直挂在第五个数据的原因是,没有dfs(nx,n ...
- CodeForces - 445A - DZY Loves Chessboard解题报告
对于这题本人刚开始的时候觉得应该用DFS来解决实现这个问题,但由于本人对于DFS并不是太熟,所以就放弃了这个想法: 但又想了想要按照这个要求实现问题则必须是黑白相间,然后把是字符是'B'或'W'改为' ...
随机推荐
- APACHE,NGINX 详细分析
Apache是目前最流行的Web应用服务器,占据了互联网应用服务器70%以上的份额.Apache能取得如此成功并不足为奇:它免费.稳定且性能卓越:但Apache能取得如此佳绩的另一个原因是,当时互联网 ...
- Androd选取相册照片和拍照处理-android学习之旅(62)
实现如下图所示效果 核心代码 -构建打开相册和拍照的Intent 拍照 File outputImage = new File(Environment.getExternalStorageDirect ...
- 【Unity Shaders】Reflecting Your World(反射吧!)介绍
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- 【Unity Shaders】Using Textures for Effects —— 实现Photoshop的色阶效果
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- ZooKeeper实现命名服务
使用场景 命名服务就是提供名称的服务,Zookeeper的命名服务有两个应用方面.一个是提供类似JNDI功能,另一个是制作分布式的序列号生成器. JNDI功能,我们利用Zookeep ...
- Linux搭建GIT 使用Eclipse创建并上传Git项目 EGit操作
Linux搭建Git 1. gitblit服务器文档 http://gitblit.com/setup_go.html 2. 安装jdk 参考 http://blog.csdn.net/jerome_ ...
- 菜鸟玩云计算之十四:克隆虚拟机及ssh登录的问题
菜鸟玩云计算之十四:克隆虚拟机及ssh登录的问题 今天早上,我的Ubuntu12.04.3LTS Desktop提示升级,升级. 从vm-ubuntu12.04克隆出虚拟机vm-thrift: $ s ...
- Live555 直播源 以及MediaSubsession
/* * H264DeviceSource.hh * * Created on: 2014年7月19日 * Author: zjzhang */ #ifndef H264DEVICESOURCE_HH ...
- 被final关键字坑了
一直都傻傻的以为用final关键字定义的都是不可变的.没想到的是对基本类型来说,这是一直成立的. 但是对于final修饰的对象,仍然可以修改对象里面的对象和成员变量.不变的只是当前对象的地址. 昨天我 ...
- [查阅]Dalvik opcodes
Dalvik opcodes Dalvik opcodes Author: Gabor Paller Vx values in the table denote a Dalvik register. ...