961C.md (1581B)
1 Source: https://codeforces.com/problemset/problem/961/C 2 3 C. Chessboard 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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 *a**k*, *i*, *j*; 1 being black and 0 being white. 22 23 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 2*n* by 2*n*. You are allowed to move pieces but not allowed to rotate or flip them. 24 25 Input 26 27 The first line contains odd integer *n* (1 ≤ *n* ≤ 100) — the size of all pieces of the board. 28 29 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. 30 31 Output 32 33 Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard. 34 35 Examples 36 37 Input 38 39 ``` 40 1 41 0 42 43 0 44 45 1 46 47 0 48 ``` 49 50 Output 51 52 ``` 53 1 54 ``` 55 56 Input 57 58 ``` 59 3 60 101 61 010 62 101 63 64 101 65 000 66 101 67 68 010 69 101 70 011 71 72 010 73 101 74 010 75 ``` 76 77 Output 78 79 ``` 80 2 81 ```