CF1881C.Perfect Square

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Kristina has a matrix of size nn by nn , filled with lowercase Latin letters. The value of nn 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 9090^\circ clockwise once.

Here is an example of rotating a matrix by 9090^\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 44 by 44 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 } $$ </p><p>then it is enough to apply $1$$$ operation to the letter b, highlighted in bold.

输入格式

The first line of the input contains a single integer tt ( 1t1021 \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 nn ( 2n1032 \le n \le 10^3 ) — the number of rows and columns in the matrix.

Then follows nn lines, each containing exactly nn lowercase Latin letters.

It is guaranteed that the sum of nn over all test cases does not exceed 10310^3 .

输出格式

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.

输入输出样例

  • 输入#1

    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

    输出#1

    1
    2
    181
    5
    9

说明/提示

The first test case is explained in the problem statement.

首页