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 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.

Input

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.

Output

Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.

Examples
Input
1
0

0

1

0
Output
1
Input
3
101
010
101

101
000
101

010
101
011

010
101
010
Output
2


题意:给你四块分裂的小正方形,然后让你凑成一个大的正方形,并且相邻的位置不能相同,然后问你最小的变化次数是多少?


解题思路:因为我们不知道这几块小正方形的排列顺序,所以我们可以通过枚举来做,然后我们也不知道第一个位置是0还是1(我们只需要知道第一个位置的数是什么,那么剩下位置的数字我们都知道了),然后就是暴力求最优答案。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=110;
char num[4][maxn][maxn];
char ch1[maxn*2][maxn*2],ch2[maxn*2][maxn*2];
int n;
int main(){
	int i,j,k;
	scanf("%d",&n);
	for(i=0;i<4;i++){
		for(j=0;j<n;j++){
			scanf("%s",num[i][j]);
		}
	}
	ch1[0][0]=1;ch2[0][0]=0;
	for(i=0;i<2*n;i++){
		for(j=0;j<2*n;j++){
			if(i==0){
				ch1[i][j]=1-ch1[i][j-1];
				ch2[i][j]=1-ch2[i][j-1];
			}
			else{
				ch1[i][j]=1-ch1[i-1][j];
				ch2[i][j]=1-ch2[i-1][j];
			}
		}
	}
	int ans=1000101;
	int x1,x2,x3,x4;
	for(x1=0;x1<4;x1++){
		for(x2=0;x2<4;x2++){
			if(x1==x2) continue;
			for(x3=0;x3<4;x3++){
				if(x2==x3||x1==x3) continue;
				for(x4=0;x4<4;x4++){
					if(x4==x1||x4==x2||x4==x3) continue;
					int ans1=0;
					for(j=0;j<n;j++){
						for(k=0;k<n;k++){
							if(num[x1][j][k]-'0'!=ch1[j][k]){
								ans1++;
							}
						}
						for(k=0;k<n;k++){
							if(num[x2][j][k]-'0'!=ch1[j][k+n]){
								ans1++;
							}
						}
					}
					for(j=0;j<n;j++){
						for(k=0;k<n;k++){
							if(num[x3][j][k]-'0'!=ch1[j+n][k]){
								ans1++;
							}
						}
						for(k=0;k<n;k++){
							if(num[x4][j][k]-'0'!=ch1[j+n][k+n]){
								ans1++;
							}
						}
					}
					if(ans1<ans){
						ans=ans1;
					} 
					ans1=0;
					for(j=0;j<n;j++){
						for(k=0;k<n;k++){
							if(num[x1][j][k]-'0'!=ch2[j][k]){
								ans1++;
							}
						}
						for(k=0;k<n;k++){
							if(num[x2][j][k]-'0'!=ch2[j][k+n]){
								ans1++;
							}
						}
					}
					for(j=0;j<n;j++){
						for(k=0;k<n;k++){
							if(num[x3][j][k]-'0'!=ch2[j+n][k]){
								ans1++;
							}
						}
						for(k=0;k<n;k++){
							if(num[x4][j][k]-'0'!=ch2[j+n][k+n]){
								ans1++;
							}
						}
					}
					if(ans1<ans) ans=ans1;
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值