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'改为' ...
随机推荐
- 【一天一道LeetCode】#91. Decode Ways
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 A messa ...
- Uva - 1607 - Gates
题目理解麻烦,估计提交量少(总共只有32个人...)也是因为题目比较麻烦,看起来像物理题,实际理解了还可以.整个电路的功能就4种,先判断x=0和x=1的输出是否相同,吐过相同,而整个电路是常熟,随便输 ...
- Web资源认证原理
Web服务器与浏览器之间的认证流程没有规定的步骤,根据不同的认证模式及鉴权方式可能会有不同的执行步骤.下图用一个最简单的流程了解整个认证过程是如何工作的,首先浏览器向服务器发起请求,然后服务器向浏览器 ...
- Linux搭建GIT 使用Eclipse创建并上传Git项目 EGit操作
Linux搭建Git 1. gitblit服务器文档 http://gitblit.com/setup_go.html 2. 安装jdk 参考 http://blog.csdn.net/jerome_ ...
- 如何成为一名优秀的web前端工程师
我所遇到的前端程序员分两种: 第一种一直在问:如何学习前端? 第二种总说:前端很简单,就那么一点东西. 我从没有听到有人问:如何做一名优秀.甚至卓越的WEB前端工程师. 何为:前端工程师? 前端工程师 ...
- C#之面向对象的特性
类是一种抽象的数据类型,但是其抽象的程度有可能会不同,而对象就是一个类的实例,例如,将花设计为一个类,天堂鸟和矢车菊就可以各为一个对象,从这里我们可以看出来,天堂鸟和矢车菊 ...
- 高通android开发摘要
一部分是开源的,可以从codeaurora.org上下载,还有一部分是高通产权的,需要从高通的网站上下载. 将高通产权的代码放到:vendor/qcom/proprietary 1. 设置bms一些参 ...
- 关于Yuri Boykov and Vladimir Kolmogorov 于2004年提出的max flow / min cut的算法的详解
出处:http://blog.csdn.net/euler1983/article/details/5959622 算法优化algorithmgraphtree任务 这篇文章说的是Yuri Boyko ...
- Linux权限与命令间的关系
极重要!权限与命令间的关系: 我们知道权限对於使用者帐号来说是非常重要的,因为他可以限制使用者能不能读取/创建/删除/修改文件或目录! 在这一章我们介绍了很多文件系统的管理命令,第六章则介绍了很多文件 ...
- hadoop任务监控页面namenode:50030(在hadoop配置中查找集群jobtracker的ip,访问50030)
公司集群,配置的hadoop.执行job,想去看看运行状态,却不知道jobtracker的机器ip: 查询hadoop 的jobtrack机器的ip,就查看文件conf/mapred-site.xml ...