#K3956. Spiral Walk Through Matrix
Spiral Walk Through Matrix
Spiral Walk Through Matrix
You are given a square matrix of size \(n \times n\). Your task is to traverse the matrix in a spiral order starting from the top-left corner and moving rightwards, then down, left, and up in a cyclic fashion until all elements are visited.
Input Format: The first line contains an integer \(n\) denoting the size of the matrix. The next \(n\) lines each contain \(n\) space-separated integers representing the rows of the matrix.
Output Format: Print the spiral order traversal of the matrix as a sequence of numbers separated by a space.
Example:
Input: 3 1 2 3 4 5 6 7 8 9</p>Output: 1 2 3 6 9 8 7 4 5
inputFormat
The input is read from standard input (stdin). The first line contains an integer \(n\), which represents the number of rows (and columns) of the square matrix. Each of the following \(n\) lines contains \(n\) space-separated integers, representing the matrix's rows.
outputFormat
The output should be written to standard output (stdout) as a single line of space-separated integers representing the elements of the matrix in spiral order.
## sample3
1 2 3
4 5 6
7 8 9
1 2 3 6 9 8 7 4 5