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'改为' ...
随机推荐
- Android读取/dev/graphics/fb0 屏幕截图
Android屏幕截图有很多方式这里只使用其中一种截图 主要是读取/dev/graphics/fb0,进行转换,复杂点就在如何把读取的数据进行转换. 可以参考一下这篇文章:http://blog.ch ...
- 【翻译】如何创建Ext JS暗黑主题之二
原文:How to Create a Dark Ext JS Theme– Part 2 我已经展示了如何去开发一个精致的暗黑主题,看起来就像Spotify.在本文的第一部分,了解了Fashion.S ...
- Dynamics CRM2013 picklist下拉项行数控制
CRM2013和前面几个版本相比有了很大的变化,本文中讲述的picklist亦然.CRM2013的picklist效果图如下所示 目前能看到的是会根据下拉内容项的数量不同而显示不同的下拉行数,但有时客 ...
- Dynamics CRM2013 业务规则的新建、激活与删除
CRM2013的一个新的feature叫做业务规则,一些页面的简单的显示隐藏的控制.字段是否必填.有条件的锁定字段.错误提示等等,以前都是需要些脚本代码实现现在只需通过业务规则做一些简单的配置就可以达 ...
- Css详解之(伪类选择器)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 开源摄影机:Axiom Camera
一般情况下只有软件才有开源这个概念.这会儿发现了个很厉害的开源的产品:开源摄影机. 我还是第一次听说摄影机也可以开源.于是去该产品的官方网站了解了一下相关信息. 官网:http://axiom.ape ...
- OC语言(二)
十.匿名对象 即不将对象赋给指针,而是直接使用 注意new对象的时候要用[ ]嵌套 例如:[[Car new] run]; 十一.枚举书写规范 注意空格 //注视 typedef enum { } X ...
- android 之ViewStub
在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局.那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE,然后在 ...
- 【Java编程】Java基本数据类型
在较前面的一篇博文<C/C++基本数据类型>中,我主要介绍了c/c++的基本数据类型.我们知道C语言没有具体规定各类数据类型所占内存的字节数,只要求long型数据长度不小于int型,sho ...
- Linux常用命令(第二版) --Shell应用技巧
Shell应用技巧 小技巧: 1.命令补全功能: <Tab>键 2.清屏: Ctrl+l 3.删除光标前所有内容: Ctrl+u 4.命令历史记录: history 这时: !histo ...