Spiral Matrix III - Problem
Spiral Matrix III - Navigate in a Clockwise Spiral Pattern
Imagine you're a robot starting at position
Here's the twist: you're allowed to walk outside the grid boundaries! When you step outside, you continue your spiral journey in the void, but you might re-enter the grid later. Think of it like walking on an infinite plane where only certain cells (the original grid) matter.
Goal: Return an array of coordinates representing the order in which you visit each cell within the original grid.
Spiral Pattern: East → South → West → North → East (repeat)
Step Pattern: 1 step east, 1 step south, 2 steps west, 2 steps north, 3 steps east, 3 steps south, 4 steps west, 4 steps north, ...
The number of steps increases every two direction changes!
Imagine you're a robot starting at position
(rStart, cStart) in an rows × cols grid, initially facing east. Your mission is to walk in a clockwise spiral pattern to visit every single cell in the grid.Here's the twist: you're allowed to walk outside the grid boundaries! When you step outside, you continue your spiral journey in the void, but you might re-enter the grid later. Think of it like walking on an infinite plane where only certain cells (the original grid) matter.
Goal: Return an array of coordinates representing the order in which you visit each cell within the original grid.
Spiral Pattern: East → South → West → North → East (repeat)
Step Pattern: 1 step east, 1 step south, 2 steps west, 2 steps north, 3 steps east, 3 steps south, 4 steps west, 4 steps north, ...
The number of steps increases every two direction changes!
Input & Output
example_1.py — Basic 1x4 Grid
$
Input:
rows = 1, cols = 4, rStart = 0, cStart = 0
›
Output:
[[0,0],[0,1],[0,2],[0,3]]
💡 Note:
Starting at (0,0) facing east, we move right through the single row: (0,0) → (0,1) → (0,2) → (0,3). The spiral naturally visits all cells in order.
example_2.py — 5x6 Grid from Center
$
Input:
rows = 5, cols = 6, rStart = 1, cStart = 4
›
Output:
[[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
💡 Note:
Starting at (1,4), the spiral expands outward: 1 step east to (1,5), 1 step south to (2,5), 2 steps west to (2,4) then (2,3), 2 steps north to (1,3) then (0,3), and so on. The pattern continues until all 30 cells are visited.
example_3.py — Single Cell Grid
$
Input:
rows = 1, cols = 1, rStart = 0, cStart = 0
›
Output:
[[0,0]]
💡 Note:
Edge case with only one cell. We start and end at (0,0) since there's nowhere else to go.
Constraints
- 1 ≤ rows, cols ≤ 100
- 0 ≤ rStart < rows
- 0 ≤ cStart < cols
- Grid size is manageable - maximum 10,000 cells
Visualization
Tap to expand
Understanding the Visualization
1
Initialize at Starting Position
Begin at (rStart, cStart) facing east, ready to start our spiral journey
2
Follow 1-1-2-2 Pattern
Move 1 step east, 1 step south, 2 steps west, 2 steps north
3
Expand to 3-3-4-4 Pattern
Continue with 3 steps east, 3 steps south, 4 steps west, 4 steps north
4
Record Valid Positions Only
Only add coordinates to result when they fall within the original grid boundaries
Key Takeaway
🎯 Key Insight: The spiral follows a predictable expanding pattern where step counts increase every two direction changes, making it easy to simulate without complex calculations.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code