Problem 227172 · medium · Phase 02 Linear Data Structures

Counter-Clockwise Spiral Fill

2d-lists · simulation · direction-changes

Build a rows x cols grid containing the numbers 1 to rows * cols laid out in a spiral that starts at the top-left corner, first moves down the first column, then right along the bottom, then up, then left, spiralling inward counter-clockwise until every cell is filled.

Examples

Input:  rows = 3, cols = 3
Output: [[1, 8, 7],
         [2, 9, 6],
         [3, 4, 5]]

Input:  rows = 2, cols = 3
Output: [[1, 6, 5],
         [2, 3, 4]]

Input:  rows = 1, cols = 1
Output: [[1]]

Constraints

  • 1 <= rows, cols <= 60

Goals

  • Fill a grid by simulating movement with direction changes
  • Turn when the next cell is out of bounds or already filled
Starting Python…