C. Chessboard time limit per test1 second memory limit per test256 megabytes inputstandard input out

C. Chessboard
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nn 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, j1 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
Copy
1
0

0

1

0
output
1
input
Copy
3
101
010
101

101
000
101

010
101
011

010
101
010
output
2

This question feels quite interesting. There are four chessboards, and each square is either 0 or 1. Let you combine four odd-numbered chessboards into one chessboard, regardless of the order. It asks you to modify at least 1 how many squares so that each square Different from adjacent grid numbers.

Let's think about it first, what does the correct chessboard look like? It is nothing more than 1 0, 1 1 appearing alternately, linked to the coordinates, if i+j is an odd number, it is a certain state, otherwise it is another state. In fact, a complete chessboard is divided into four, and then they are combined to produce two kinds of chessboards, the first one is 0 or the first one is 1.

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;

char s[4][150][150];
char mp[250][250];
int ii[] = {0,1,2,3};
int n;


int main()
{
	cin >> n;
	for(int i = 0; i < 4; i++)
	for(int j = 0; j < n; j++)
	for(int k = 0; k < n; k++)
	    cin >> s[i][j][k];
	int ans = 1e8;
	do{
		int tmp = 0;
		for(int i = 0; i < 2*n; i++)
		for(int j = 0; j < 2*n; j++)
		{
  	        if(i <= n && j <= n)
			{
				mp[i][j] = s[ii[0]][i][j];
			}
			if(i <= n && j > n)
			{
				mp [i] [j] = s [ii [1]] [i] [jn];
			}
			if(i > n && j <= n)
			{
				mp [i] [j] = s [ii [2]] [in] [j];
			}
			if(i > n && j > n)
			{
				mp [i] [j] = s [ii [3]] [in] [jn];
			}
			char c = (i+j) % 2 ? '1' : '0';
			if(c != mp[i][j]) tmp++;
		}
		years = min(years,tmp);
	}while(next_permutation(ii,ii+4));
	cout << ans <<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325806766&siteId=291194637