Perfect Square (Codeforces 1881C)
Tags: Implement, brute force

Kristina has a matrix of size $$$n$$$ by $$$n$$$, filled with lowercase Latin letters. The value of $$$n$$$ is even.She wants to change some characters so that her matrix becomes a perfect square. A matrix is called a perfect square if it remains unchanged when rotated $$$90^\circ$$$ clockwise once.Here is an example of rotating a matrix by $$$90^\circ$$$:  In one operation, Kristina can choose any cell and replace its value with the next character in the alphabet. If the character is equal to "z", its value does not change.Find the minimum number of operations required to make the matrix a perfect square.For example, if the $$$4$$$ by $$$4$$$ matrix looks like this:$$$$$$\matrix{ a & b & b & a \cr b & c & \textbf{b} & b \cr b & c & c & b\cr a & b & b & a \cr }$$$$$$then it is enough to apply $$$1$$$ operation to the letter b, highlighted in bold.

Input: The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^2$$$) — the number of test cases.Then follows the description of each test case.The first line of each test case contains a single even integer $$$n$$$ ($$$2 \le n \le 10^3$$$) — the number of rows and columns in the matrix.Then follows $$$n$$$ lines, each containing exactly $$$n$$$ lowercase Latin letters.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$.

Output: For each test case, output a single number on a separate line: the minimum number of operations required for Kristina to obtain a perfect square.

Note: The first test case is explained in the problem statement.

Sample Inputs:
5
4
abba
bcbb
bccb
abba
2
ab
ba
6
codefo
rcesco
deforc
escode
forces
codefo
4
baaa
abba
baba
baab
4
bbaa
abba
aaba
abba

Sample Outputs:
1
2
181
5
9

Solution (Python):
def check():
    n = int(input())
    mat = []
    for i in range(n):
        mat.append(input())
    rsum = 0
    for i in range(n // 2):
        for j in range(i, n - i - 1):
            ele = [ord(mat[i][j]), ord(mat[j][n - 1 - i]), ord(mat[n - 1 - i][n - 1 - j]), ord(mat[n - 1 - j][i])]
            rsum += max(ele) * 4 - sum(ele)
    print(rsum)
t = int(input())
for i in range(t):
    check()

Solution (Python):
t = int(input())
for _ in range(t):
    n = int(input())
    matrix = [[ord(x) for x in input()] for _ in range(n)]
    ans = 0
    for i in range(n // 2):
        for j in range(n // 2):
            ans += max(matrix[i][j], matrix[j][~i], matrix[~i][~j], matrix[~j][i])
    print(ans * 4 - sum(map(sum, matrix)))

Solution (Python):
P = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
for _ in range(int(input())):
    N = int(input())
    A = [input() for _ in range(N)]
    t = 0
    for i in range(N // 2):
        for j in range(N // 2):
            K = [P[A[i][j]], P[A[j][~i]], P[A[~i][~j]], P[A[~j][i]]]
            t += 4 * max(K) - sum(K)
    print(t)