Codeforces Round 903 (Div. 3) Question C perfect square

Title

title

Input and output examples
Enter:

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

Analysis of question meaning

Each figure can be divided into four parts. Only one part is Yuanshi Tianzun, and the others are generated by this part rotating around the center point of nothingness.
It seems that each letter of Yuanshi Tianzun can reach the position of the other three letters through rotation, so there are actually four positions behind each letter of Yuanshi Tianzun.
Therefore, the problem is simplified to traversing the four positions of each letter of Yuanshi Tianzun, taking the maximum value of the letters at the four positions, and then calculating how many steps it takes to turn all the letters at these four positions into this letter.

Code summary

It’s involved here

n

?

n

The problem of rotation of elements within a square matrix can be summarized. make

this

n

?

n

The two-dimensional array of square matrix is

a

[

n

]

[

n

]

So, if

a

[

i

]

[

j

]

If you press

Rotate clockwise four times and return to the original position. His four positions are:

a

[

i

]

[

j

]

,

a

[

j

]

[

n

?

1

?

i

]

,

a

[

n

?

1

?

i

]

[

n

?

1

?

j

]

,

a

[

n

?

1

?

j

]

[

i

]

If it rotates counterclockwise

4

Returning to the original position for the first time, his four positions are:

a

[

i

]

[

j

]

,

a

[

n

?

1

?

j

]

[

i

]

,

a

[

n

?

1

?

i

]

[

n

?

1

?

j

]

,

a

[

j

]

[

n

?

1

?

i

]

To make a simple summary, let

i

or

j

conduct

n

?

1

?

i

/

n

?

1

?

j

The operation is called

f

(

i

)

or

f

(

j

)

, then clockwise means continuously

x

=

y

,

y

=

f

(

x

)

Counterclockwise means proceeding continuously

y

=

x

,

x

=

f

(

y

)

operation, applicable to any

n

This involves the rotation of elements within an n*n square matrix, which can be summarized. Let\The two-dimensional array of n*n square matrix is a[n][n]. Then, if a[i][j] is rotated four times in the clockwise direction and returns to the original position. , his four positions are: \a[i][j], a[j][n-1-i], a[n-1-i][n-1-j], a [n-1-j][i]\If he rotates counterclockwise 4 times and returns to the original position, his four positions are:\a[i][j], a[n- 1-j][i], a[n-1-i][n-1-j], a[j][n-1-i]\To make a simple summary, let for i or j The operation of n-1-i/n-1-j\ is called f(i) or f(j), then clockwise means x=y,y=f(x)\ Counterclockwise means continuously performing y=x,x=f(y) operations, which is applicable to any n

This involves the rotation of elements within the n?n square matrix, which can be summarized. Let the two-dimensional array of this n?n square matrix be a[n][n]. Then, if a[i][j] is rotated four times in the clockwise direction and returned to its original position, its four positions are: a[i][j], a[j][n?1?i], a[n?1?i][n?1?j], a[n?1?j][i] if he is Rotate counterclockwise 4 times and return to the original position. Its four positions are: a[i][j], a[n?1?j][i], a[n?1?i][n?1? j], a[j][n?1?i] is a simple summary, let the operation of n?1?i/n?1?j for i or j be called f(i) or f(j) , then clockwise means continuously performing x=y,y=f(x) counterclockwise means continuously performing y=x,x=f(y) operations, applicable to any n

Code display and analysis

Let’s borrow jiangly’s code here. The code is very clear and simple.

#include <bits/stdc + + .h>
using i64 = long long;
void solve() {
    int n;
    std::cin >> n;
    std::vector<std::string> s(n);
    for (int i = 0; i < n; i + + ) {
        std::cin >> s[i];
    }
    
    int ans = 0;
    for (int i = 0; i < n / 2; i + + ) {
        for (int j = 0; j < n / 2; j + + ) {
            int sum = s[i][j] + s[j][n - 1 - i] + s[n - 1 - i][n - 1 - j] + s[n - 1 - j][i] ;
            int max = std::max({s[i][j], s[j][n - 1 - i], s[n - 1 - i][n - 1 - j], s[n - 1 - j][i]});
            ans + = max * 4 - sum;
        }
    }
    std::cout << ans << "\\
";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

Analysis of key code parts

 for (int i = 0; i < n / 2; i + + ) {
        for (int j = 0; j < n / 2; j + + ) {
            int sum = s[i][j] + s[j][n - 1 - i] + s[n - 1 - i][n - 1 - j] + s[n - 1 - j][i] ;
            int max = std::max({s[i][j], s[j][n - 1 - i], s[n - 1 - i][n - 1 - j], s[n - 1 - j][i]});
            ans + = max * 4 - sum;
        }
    }
    std::cout << ans << "\\
";

This part has two floors

f

o

r

Looping is traversing

n

?

n

upper left corner of the square

(

i

and

j

average

< n 2 ) for each element of the section, and then access all of their other letters against those The letter size of the position, and at the same time calculate the number of operations required if you want to unify it. This part of the two-layer for loop traverses every element in the upper left corner of the n*n square matrix (i and j are both <\frac{n}2)\, and then accesses all other elements of these letters. The size of the letters at the \ position, and at the same time calculate the number of operations required if you want to unify it. This part of the two-layer for loop traverses every element in the upper left corner of the n?n square matrix (i and j are both <2n?), and then accesses the letter sizes of all other positions of these letters, and at the same time calculates if To unify, the number of operations required

Tell me important things again

This conclusion is still very good

It’s involved here

n

?

n

The problem of rotation of elements within a square matrix can be summarized. make

this

n

?

n

The two-dimensional array of square matrix is

a

[

n

]

[

n

]

So, if

a

[

i

]

[

j

]

If you press

Rotate clockwise four times and return to the original position. His four positions are:

a

[

i

]

[

j

]

,

a

[

j

]

[

n

?

1

?

i

]

,

a

[

n

?

1

?

i

]

[

n

?

1

?

j

]

,

a

[

n

?

1

?

j

]

[

i

]

If it rotates counterclockwise

4

Returning to the original position for the first time, his four positions are:

a

[

i

]

[

j

]

,

a

[

n

?

1

?

j

]

[

i

]

,

a

[

n

?

1

?

i

]

[

n

?

1

?

j

]

,

a

[

j

]

[

n

?

1

?

i

]

To make a simple summary, let

i

or

j

conduct

n

?

1

?

i

/

n

?

1

?

j

The operation is called

f

(

i

)

or

f

(

j

)

, then clockwise means continuously

x

=

y

,

y

=

f

(

x

)

Counterclockwise means proceeding continuously

y

=

x

,

x

=

f

(

y

)

operation, applicable to any

n

This involves the rotation of elements within an n*n square matrix, which can be summarized. Let\The two-dimensional array of n*n square matrix is a[n][n]. Then, if a[i][j] is rotated four times in the clockwise direction and returns to the original position. , his four positions are: \a[i][j], a[j][n-1-i], a[n-1-i][n-1-j], a [n-1-j][i]\If he rotates counterclockwise 4 times and returns to the original position, his four positions are:\a[i][j], a[n- 1-j][i], a[n-1-i][n-1-j], a[j][n-1-i]\To make a simple summary, let for i or j The operation of n-1-i/n-1-j\ is called f(i) or f(j), then clockwise means x=y,y=f(x)\ Counterclockwise means continuously performing y=x,x=f(y) operations, which is applicable to any n

This involves the rotation of elements within the n?n square matrix, which can be summarized. Let the two-dimensional array of this n?n square matrix be a[n][n]. Then, if a[i][j] is rotated four times in the clockwise direction and returned to its original position, its four positions are: a[i][j], a[j][n?1?i], a[n?1?i][n?1?j], a[n?1?j][i] if he is Rotate counterclockwise 4 times and return to the original position. Its four positions are: a[i][j], a[n?1?j][i], a[n?1?i][n?1? j], a[j][n?1?i] is a simple summary, let the operation of n?1?i/n?1?j for i or j be called f(i) or f(j) , then clockwise means continuously performing x=y,y=f(x) counterclockwise means continuously performing y=x,x=f(y) operations, applicable to any n