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'改为' ...
随机推荐
- 存储那些事儿(三):OpenStack的块存储Cinder与商业存储的融合
OpenStack是一个美国国家航空航天局和Rackspace合作研发的云端运算软件,以Apache许可证授权,并且是一个自由软件和开放源代码项目.OpenStack是IaaS(基础设施即服务)软 ...
- Socket层实现系列 — 睡眠驱动的同步等待
主要内容:Socket的同步等待机制,connect和accept等待的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 概述 socket上定义了 ...
- Hibernate与Spring的事务管理
什么是事务 这个问题比较大,按照我的理解就是,一个事务内的n个操作,要么全部完成,一旦有一个操作有问题,那么所有的操作都全部回滚. Jdbc的事务 首先,大家已经知道了,事务说白了就是一个词----统 ...
- Cannot start service MSSQL$MICROSOFT##WID on computer
在做ADFS部署过程中配置ADFS服务时遇到如下问题 检查系统日志错误日志如下,很明显"NT SERVICE\MSSQL$MICROSOFT##WID"这个账户不在log on a ...
- 17_Android中Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数,传递参数的时候指定权限
1 Broadcast是Android中的四大组件之一,他的用途很大,比如系统的一些广播:电量低.开机.锁屏等一些操作都会发送一个广播. 2 广播被分为两种不同的类型:"普通广播( ...
- SpriteBuilder给节点添加effect在32设备上发生crash
环境为 Xcode 6.4 , cocos2D 3.0.4 , SpriteBuilder 1.4.9 在给某一节点添加Effect后,运行在真机iphone4s上发生崩溃,显示为: 可以看到整个堆栈 ...
- Java进阶(九)正则表达式
java正则表达式 序 由于项目中使用到了利用正则表达式进行表单的校验,回想一下正则表达式的内容,忘得也差不多了,俗话说:"温故而知新,可以为师矣".今天就简单的温故一下正则表达式 ...
- mysql进阶(九)多表查询
MySQL多表查询 一 使用SELECT子句进行多表查询 SELECT 字段名 FROM 表1,表2 - WHERE 表1.字段 = 表2.字段 AND 其它查询条件 SELECT a.id,a.na ...
- Leetcode_62_Unique Paths
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43404205 A robot is located at ...
- android 面试之listview
ListView优化一直是一个老生常谈的问题,不管是面试还是平常的开发中,ListView永远不会被忽略掉,那么这篇文章我们来看看如何最大化的优化ListView的性能.· 1.在adapter中的g ...