#K52567. Extracting Anti-Diagonals in a Square Matrix

    ID: 29338 Type: Default 1000ms 256MiB

Extracting Anti-Diagonals in a Square Matrix

Extracting Anti-Diagonals in a Square Matrix

You are given a square matrix of integers of size \(N \times N\). Your task is to extract all the anti-diagonals of the matrix. An anti-diagonal is defined as a set of elements starting from a position in the upper row or the rightmost column and moving downwards while moving leftwards.

The extraction is performed in two phases:

  • Upper Triangle (including the main diagonal): For each column of the first row, traverse downwards by increasing the row index and decreasing the column index.
  • Lower Triangle (excluding the main diagonal): For each row starting from the second row, traverse similarly starting from the last column.

For example, given the matrix: \[ \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \] The anti-diagonals are: \[ [1],\ [2, 4],\ [3, 5, 7],\ [6, 8],\ [9] \]

Read the input from stdin and write the output to stdout as described below.

inputFormat

The input starts with an integer \(T\) denoting the number of test cases. For each test case:

  • The first line contains an integer \(N\), the size of the matrix.
  • The following \(N\) lines each contain \(N\) space-separated integers representing the rows of the matrix.

All input is provided via stdin.

outputFormat

For each test case, output the anti-diagonals of the matrix. Each anti-diagonal should be printed on its own line with its elements separated by a single space. The output for different test cases immediately follows one another. All output should be written to stdout.

## sample
2
3
1 2 3
4 5 6
7 8 9
2
1 2
3 4
1

2 4 3 5 7 6 8 9 1 2 3 4