Spiral Matrix Traversal
Ashdown Logistics audits its warehouse floor with a robot that starts in the north-west corner and drives the perimeter -- east along the top aisle, south down the far side, west along the bottom, north back up the near side -- then steps one lane inward and goes round again, until no bay is left. The floor is a rectangle of bays, each holding a stock count, and the audit report has to list those counts in the exact order the robot reaches them.
Task: Print every bay's stock count in the order the robot visits them.
Input
The first line holds two integers rows and cols separated by a single space, with 1 <= rows <= 100 and 1 <= cols <= 100. Each of the next rows lines holds cols integers separated by single spaces.
Output
One line holding all rows x cols values in visiting order, separated by single spaces. Every bay appears exactly once.
Example:
Input:
3 3
1 2 3
4 5 6
7 8 9
Output:
1 2 3 6 9 8 7 4 5
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in