Spiral Matrix III
You start at cell (rStart, cStart) in a rows x cols grid, facing east, and walk in a clockwise spiral. You keep spiraling outward, and whenever your path leaves the grid you still walk those steps but skip recording out-of-bounds cells. Return the coordinates of all rows*cols grid cells in the order you first visit them.
Open official problem prompt ↗Emit every grid cell in the order an outward clockwise spiral starting at an arbitrary cell would first touch it, ignoring steps that land outside the grid.
Like a lighthouse beam sweeping in ever-widening square loops from where you stand: you keep circling outward, and you only note the buildings that are actually inside the city limits.
- Input
- rows = 1, cols = 4, rStart = 0, cStart = 0
- Output
- [[0,0],[0,1],[0,2],[0,3]]
- Why
- From (0,0) the outward spiral first records the two in-bounds cells to the east, and after looping around it eventually records (0,2) and (0,3), covering all 4 cells of the single row.
1 <= rows, cols <= 1000 <= rStart < rows0 <= cStart < cols