114. Spiral Walk in the Garden

Acceptance — 0 accepted 0 submissions

Medium
MatrixSimulation
No companies listed
A gardener waters a rectangular field of plants in a spiral: starting from the top-left corner, she walks right, then down, then left, then up, moving one layer inward each round.

Print the plant numbers in the order she waters them. This is your final boss for this level. You've got this!

Input Format
The first line has r and c.
Each of the next r lines has c integers.

Output Format
Print all r × c values in spiral order, separated by single spaces.

Constraints
• 1 ≤ r, c ≤ 100
• -1000 ≤ value ≤ 1000

Example 1

Input
3 3
1 2 3
4 5 6
7 8 9
Output
1 2 3 6 9 8 7 4 5

Explanation Right 1 2 3, down 6 9, left 8 7, up 4, then the centre 5.

Example 2

Input
3 4
1 2 3 4
5 6 7 8
9 10 11 12
Output
1 2 3 4 8 12 11 10 9 5 6 7

Explanation The outer ring first, then the inner part 6 7.

Example 3

Input
1 4
1 2 3 4
Output
1 2 3 4

Explanation A single row is just read left to right.

Example 4

Input
3 1
1
2
3
Output
1 2 3

Explanation A single column is read top to bottom.

Acceptance —
Accepted 0
Submissions 0
Auto-save
Saved Ln 1, Col 1
Submit a solution to see its status here.
Input
Expected Output
1 2 3 6 9 8 7 4 5
No results yet
Run your code to test it against the sample cases.
Problem List