An observatory ceiling is tiled as an R-by-C grid, each tile bearing an integer id. A cleaning robot scans the tiles in a clockwise inward spiral: it starts at the top-left tile and moves right along the top edge, then down the right edge, then left along the bottom edge, then up the left edge, and continues spiralling inward until every tile has been scanned exactly once.
Print the tile ids in the order the robot scans them.
Line 1: two integers R and C.
Next R lines: each contains C space-separated integers (one grid row).
A single line: all R*C tile ids in spiral scan order, space-separated.
Example 1
Input
3 3 1 2 3 4 5 6 7 8 9
Expected
1 2 3 6 9 8 7 4 5
Explanation
Top row left-to-right (1 2 3), right column down (6 9), bottom row right-to-left (8 7), left column up (4), then the center (5).
Example 2
Input
1 4 1 2 3 4
Expected
1 2 3 4
Explanation
A single row is scanned straight across: 1 2 3 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →