64. Spiral Traversal
Challenge1000 ms256 MBSolved by 0%
Print every value of the matrix in spiral order, left to right along the top row, then down the right column, then right to left along the bottom row, then up the left column, and inward, separated by single spaces on one line.
For:
1 2 3
4 5 6
7 8 9
the answer is 1 2 3 6 9 8 7 4 5.
Constraints - `1 ≤ r, c ≤ 100` - `-1000000 ≤ each value ≤ 1000000`
Input
The first line contains two integers r and c.
Each of the next r lines contains c space-separated integers.
Output
Print all r × c values on ONE line in spiral order, separated by single spaces.
Input3 3
1 2 3
4 5 6
7 8 9
Output1 2 3 6 9 8 7 4 5
Noteis the worked example from the statement
Input2 3
1 2 3
4 5 6
Output1 2 3 6 5 4
Noteis wider than it is tall
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.