Mirror Grid (Codeforces 1703E)
Tags: Implement, implementation

You are given a square grid with $$$n$$$ rows and $$$n$$$ columns. Each cell contains either $$$0$$$ or $$$1$$$. In an operation, you can select a cell of the grid and flip it (from $$$0 \to 1$$$ or $$$1 \to 0$$$). Find the minimum number of operations you need to obtain a square that remains the same when rotated $$$0^{\circ}$$$, $$$90^{\circ}$$$, $$$180^{\circ}$$$ and $$$270^{\circ}$$$.The picture below shows an example of all rotations of a grid.  

Input: The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the size of the grid.Then $$$n$$$ lines follow, each with $$$n$$$ characters $$$a_{i,j}$$$ ($$$0 \leq a_{i,j} \leq 1$$$) — the number written in each cell.

Output: For each test case output a single integer  — the minimum number of operations needed to make the square look the same rotated $$$0^{\circ}$$$, $$$90^{\circ}$$$, $$$180^{\circ}$$$ and $$$270^{\circ}$$$.

Note: In the first test case, we can perform one operations to make the grid $$$\begin{matrix}0 & 1 & 0\\ 1 & 1 & \color{red}{1}\\ 0 & 1 & 0\end{matrix}$$$. Now, all rotations of the square are the same.In the second test case, all rotations of the square are already the same, so we don't need any flips.

Sample Inputs:
5
3
010
110
010
1
0
5
11100
11011
01011
10011
11000
5
01000
10101
01010
00010
01001
5
11001
00000
11111
10110
01111

Sample Outputs:
1
0
9
7
6

Solution (Python):
from collections import defaultdict

def main():
    n = int(input())
    l = []
    for i in range(n):
        l.append(input())
    i = 0
    c = 0
    j = 0
    while j < n - 1:
        i = j
        while i < n - j - 1:
            lst = [l[j][i], l[i][n - j - 1], l[n - j - 1][n - 1 - i], l[n - 1 - i][j]]
            mini = min(lst.count('0'), lst.count('1'))
            c += mini
            i += 1
        j += 1
    print(c)
for i in range(int(input())):
    main()

Solution (Python):
for t in range(int(input())):
    n = int(input())
    a = []
    for i in range(n):
        a.append(str(input()))
    kq = 0
    if n % 2 == 0:
        for i in range(0, n // 2):
            for j in range(0, n // 2):
                b = [a[i][j], a[j][n - 1 - i], a[n - 1 - i][n - 1 - j], a[n - 1 - j][i]]
                if b.count('1') == 3 or b.count('1') == 1:
                    kq += 1
                if b.count('1') == 2:
                    kq += 2
        print(kq)
    if n % 2 == 1:
        for i in range(0, n // 2):
            for j in range(0, n // 2):
                b = [a[i][j], a[j][n - 1 - i], a[n - 1 - i][n - 1 - j], a[n - 1 - j][i]]
                if b.count('1') == 3 or b.count('1') == 1:
                    kq += 1
                if b.count('1') == 2:
                    kq += 2
        for i in range(n // 2):
            b = [a[n // 2][i], a[i][n // 2], a[n // 2][n - 1 - i], a[n - 1 - i][n // 2]]
            if b.count('1') == 3 or b.count('1') == 1:
                kq += 1
            if b.count('1') == 2:
                kq += 2
        print(kq)

Solution (Python):
def solveodd(a, n):
    ans = 0
    for j in range(int((n + 1) / 2)):
        for k in range(int((n - 1) / 2)):
            b = 0
            if a[j][k] == '1':
                b += 1
            if a[n - 1 - k][j] == '1':
                b += 1
            if a[n - 1 - j][n - 1 - k] == '1':
                b += 1
            if a[k][n - 1 - j] == '1':
                b += 1
            if b == 1 or b == 3:
                ans += 1
            elif b == 2:
                ans += 2
    return ans

def solveeven(a, n):
    ans = 0
    for j in range(int(n / 2)):
        for k in range(int(n / 2)):
            b = 0
            if a[j][k] == '1':
                b += 1
            if a[n - 1 - k][j] == '1':
                b += 1
            if a[n - 1 - j][n - 1 - k] == '1':
                b += 1
            if a[k][n - 1 - j] == '1':
                b += 1
            if b == 1 or b == 3:
                ans += 1
            elif b == 2:
                ans += 2
    return ans
t = int(input())
for i in range(t):
    n = int(input())
    a = []
    for j in range(n):
        a.append(input())
    if n % 2 == 1:
        print(solveodd(a, n))
    else:
        print(solveeven(a, n))