Matrix Transformations (APCS 2016-03 Intermediate)

Points 100 1.0s 256M

A matrix arranges elements in a rectangle. Its horizontal lines are rows and its vertical lines are columns. We write \(X_{i,j}\) for the element in row \(i\), column \(j\) of matrix \(X\). For example, \(X_{3,2}=6\) in Figure 1.

We define two matrix operations:

  • Flip: exchange the first and last rows, the second and second-to-last rows, and so on.
  • Rotate: rotate the entire matrix \(90\) degrees clockwise.

Figure 1 shows that flipping \(X\) produces \(Y\), and then rotating \(Y\) produces \(Z\).

A sequence of flips and rotations transforms an original matrix \(A\) into a new matrix \(B\). In Figure 2, \(A\) is flipped and then rotated twice to obtain \(B\).

You are given the final matrix \(B\) and the operations that were originally applied to \(A\), in their original order. Recover matrix \(A\).

Input

The first line contains three integers \(R\), \(C\), and \(M\): the number of rows of \(B\), the number of columns of \(B\), and the number of operations.

The next \(R\) lines each contain \(C\) integers. The \(j\)-th integer on the \(i\)-th line is \(B_{i,j}\).

The last line contains \(M\) integers \(m_1,m_2,\ldots,m_M\), describing the operations originally applied to \(A\) in order:

  • \(m_k=0\): rotate \(90\) degrees clockwise.
  • \(m_k=1\): flip vertically by reversing the order of the rows.

Constraints

  • \(1 \le R,C,M \le 10\)
  • \(0 \le B_{i,j} \le 9\)
  • \(m_k\in\{0,1\}\)
  • All input values are integers.

Output

On the first line, print two integers \(R'\) and \(C'\), the number of rows and columns of the original matrix \(A\).

Then print the \(R'\) rows of \(A\), with \(C'\) integers on each row. Separate adjacent integers by exactly one space and do not print a trailing space at the end of a row.

This problem uses strict output comparison, so follow the required format exactly.

Scoring

The two sample cases are judged but worth 0 points. There are exactly \(20\) scored groups worth \(5\) points each:

  • In \(6\) groups (\(30\) points), every operation is a flip.
  • The remaining \(14\) groups (\(70\) points) have no additional constraints.

Sample Input 1

3 2 3
1 1
3 1
1 2
1 0 0

Sample Output 1

3 2
1 1
1 3
2 1

Sample Explanation 1

The transformation is shown in Figure 2.

Sample Input 2

3 2 2
3 3
2 1
1 2
0 1

Sample Output 2

2 3
2 1 3
1 2 3

Sample Explanation 2

To recover the original matrix, undo the operations starting from the last one, as shown below.

Source

APCS March 2016 implementation problem 2; statement and figures referenced from ZeroJudge b965 “Matrix Transformations”.

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.