Location>code7788 >text

AtCoder Beginner Contest 375 C Questions (python solution)

Popularity:233 ℃/2024-10-23 21:13:53

Panasonic Programming Contest 2024(AtCoder Beginner Contest 375)C - Spiral Rotation(pythonacrobatic display (esp. on horseback) (old))**

Link to original question:

[(/contests/abc375/tasks/abc375_c)]

Title in brief:

This question asks for a specific spiral rotation operation to be performed on an N x N grid, where this N is always an even number. Here, each cell in the grid can be black (with a# (indicated by) or white (indicated by. (Representation). We need to finally output the color state of the mesh through a series of operations based on the description of the topic.

Thoughts:

insofar asi =1 until (a time)N/2In eachicarry out(i +1) %4 times rotated. That is to say:i =1 Performs 2 rotations when -i =2 When performing 3 rotations -i =3 When executing 0 rotations -i =4 1 rotation at the time of execution - and so on ......

Rotation is defined as moving the corresponding color value from one position to another.

python code

def func(N, i, A).
    r = N-1-i# Calculate the right boundary of the current layer.
    for j in range(i,r):# Perform a swap of color values at the four positions to achieve a spiral rotation
        (A[j][r], A[r][N-1-j], A[N-1-j][i], A[i][j]) = (A[i][j], A[j][r], A[r][N-1-j], A[N-1-j][i])
def main().
    N = int(input())
    A = [list(input()) for _ in range(N)]# read grid data
    for i in range(N/2):# iterate through each level, from outer to inner (total N/2 levels)
        for j in range((i+1)%4):# Calculate rotations based on current layer i
            func(N, i, A)
    print("\n".join("".join(i) for i in A))# Output the final state of the grid, converting each row to a string and concatenating it with newline characters
main()

customizablefunc(N, i, A) Function.

  • i is the left boundary of the current layer.r It's the right border.
    The rotation is achieved by a loop that swaps the color values of the four positions of the current layer.

  • The logic is as follows.

    A[i][j](top left) Swap toA[j][r](top right)

    A[j][r]swap toA[r][N-1-j](lower right)

    A[r][N-1-j]swap toA[N-1-j][i](lower left)

    A[N-1-j][i]swap toA[i][j](top left)

Finally, a complexity analysis is performed:

The rotation operation for each layer is O(N/2) because the rotation exchange for each layer involves only the elements of that layer.

# To all of you ACCCCCCCCCC*