14890 - Red Cape Flying Cat's Matrix Operations   

Description

Story

(The following story is irrelevant to the problem description.)

Red Cape Flying Cat is a sophomore cat majoring in Computer Science at NTHU. He withdrew from "Calculus II" during his freshman year.

Due to some unexpected circumstances, he was unable to enroll in required courses such as "Computer Architecture" or "Probability", and he couldn't even get into "Graph Theory", the course he wanted to take the most. With no credits to his name, he has returned to "Introduction to Programming (II)" to learn, pretending as if he has plenty of credits.

Although he has already taken "Introduction to Programming (II)" once, he used AI to complete all his assignments last semester, so he has no idea how to solve this problem. However, Red Cape Flying Cat is very well-connected and knows you—a student also taking "Introduction to Programming (II)" who completes every assignment entirely on your own without using AI. Please help him complete this assignment.

 

Problem Statement

Red Cape Flying Cat has an r x c matrix consisting only of characters @ and ..

Red Cape Flying Cat wants to perform q operations on this matrix in the given order. There are three types of operations:

  • 1: Transpose the entire matrix across the main diagonal.

  • 2 idx shift: Perform a cyclic shift on the idx-th row to the right shift times.

  • 3 idx shift: Perform a cyclic shift on the idx-th column downwards shift times.

Definition of the “single cyclic shift” operation for types 2 and 3: every element moves one position in the specified direction, and the element that moves out of the boundary is moved back to the beginning of the row or column. You can refer to the sample case for a better understanding.

Note that after a type 1 operation, the number of rows and columns may change. All subsequent operations are based on the new dimensions.

After all operations are completed, please output the final state of the matrix.

Sample Explanation

The following provides an explanation for the first test case in the sample input.

Input

The first line of the input contains a single integer t (1 <= t <= 20) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers r and c (1 <= r, c <= 50) — the number of rows and the number of columns of the matrix.

The following r lines each contain a string of c characters. The j-th character of the i-th string represents the element at the i-th row and j-th column of the matrix. Each character is either @ or ..

The next line contains a single integer q (0 <= q <= 50) — the number of operations.

The following q lines describe the operations. Each line starts with an integer representing the type of operation (1, 2, or 3). If the type is 1, there are no additional integers. If the type is 2 or 3, it is followed by two integers idx and shift (1 <= shift <= 10^9), representing the index of the row/column and the number of times to shift.

Let the current matrix size be r' x c'. For a type 2 operation, it is guaranteed that 1 <= idx <= r'. For a type 3 operation, it is guaranteed that 1 <= idx <= c'.

Output

For each test case, output the final state of the matrix after all operations are completed. Let r' and c' be the final number of rows and columns. Output r' lines, each containing a string of c' characters representing the matrix.

Don't forget to add a line break at the end.

Sample Input  Download

Sample Output  Download




Discuss