#C866. Image Flipping

    ID: 52666 Type: Default 1000ms 256MiB

Image Flipping

Image Flipping

You are given a 2D matrix (image) consisting of integer values and a string indicating the direction of the flip. The flip operation can be performed either horizontally or vertically.

Horizontal flip: Each row of the matrix is reversed.

Vertical flip: The order of the rows is reversed.

The task is to output the resulting matrix after applying the specified flip.

Use the following LaTeX formulas to describe the operations:

Horizontal flip: \(A[i][j] \rightarrow A[i][c-j-1]\) for \(0 \leq i < r, 0 \leq j < c\).

Vertical flip: \(A[i][j] \rightarrow A[r-i-1][j]\) for \(0 \leq i < r, 0 \leq j < c\).

inputFormat

The input is given via standard input (stdin) and has the following format:

→ The first line contains two integers \(r\) and \(c\) representing the number of rows and columns of the matrix.
→ The next \(r\) lines each contain \(c\) space-separated integers representing the matrix rows.
→ The last line contains a single string which is either "horizontal" or "vertical", indicating the direction to flip the matrix.

outputFormat

Output the flipped matrix to standard output (stdout). Each row of the matrix should be printed on a new line, with the integers separated by a single space.

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

6 5 4 9 8 7

</p>