A mosaic artist numbers the tiles of an R by C grid by walking a clockwise spiral. Starting at the top-left corner and heading right, they place the numbers 1, 2, 3, ... one per tile, turning clockwise (right, then down, then left, then up, and so on) whenever the next tile would leave the grid or is already numbered, until all R * C tiles are numbered.
A single line with two integers R and C.
Print R lines, each with C space-separated integers: the completed grid.
Example 1
Input
3 3
Expected
1 2 3 8 9 4 7 6 5
Explanation
Numbers spiral clockwise from the top-left: the outer ring is 1..8 and the center is 9.
Example 2
Input
1 4
Expected
1 2 3 4
Explanation
A single row is simply numbered left to right as 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 →