Matrices and grids · 10 / 15 Exercise Hard

Spiral traversal

Objective

Traversing a matrix in a spiral means reading the first row, going down the last column, coming back along the last row and going up the first column, and repeating inwards. An elegant trick: take out the first row and rotate the rest.

Instructions

  • The first line has the number of rows f and columns c; then come f lines with the values separated by spaces.
  • Show all the elements in spiral order (starting at the top left, going right), separated by spaces.
Input
3 3
1 2 3
4 5 6
7 8 9
Output
1 2 3 6 9 8 7 4 5

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise