Unlike the earlier row-by-row patterns, the spiral winds around the edge of the grid and curls inward, like water draining around a square basin.
Trace the path for a 3 by 3 grid. The number 1 starts in the top-left corner. From there the count moves right across the top row to 3, turns and moves down the right column to 5, turns again and runs left across the bottom row to 7, then climbs up the left column to 8. That single loop traces the entire outer ring. Only the center is left, so 9 goes in the middle and the grid is full.
The difficulty is the turning, not the arithmetic. After each straight run the direction changes, and the region you can still write into shrinks, since the ring you just filled is now off limits. Getting it right depends on knowing when to turn and never stepping onto a cell already filled. Two clean ways to handle that follow.
1 <= n <= 50 → n is at least 1, so there is always at least the single-cell grid [[1]]. The square shape means rows and columns share the same length n.1 to n^2 → Every cell gets a distinct value, and the largest, n^2, is at most 2500, which sits well inside a 32-bit integer.Picture four walls around the part of the grid still waiting to be filled: a top row, a bottom row, a left column, and a right column. The spiral sweeps across the top wall, down the right wall, back along the bottom wall, and up the left wall. That completes one ring.
Once a wall has been used, its cells are filled, so pull that wall inward by one: the top wall moves down, the right wall moves left, and so on. The walls keep closing in until they cross, at which point every cell has a number. A single counter, increasing from 1 to n^2, supplies the value for each cell.
n by n grid, and set top = 0, bottom = n - 1, left = 0, right = n - 1, with a counter starting at 1.top <= bottom and left <= right, do the following four passes.left to right, then move top down by one.top to bottom, then move right left by one.top <= bottom, fill the bottom row from right to left, then move bottom up by one.left <= right, fill the left column from bottom to top, then move left right by one.Input:
The walls begin at top = 0, bottom = 2, left = 0, right = 2. The top row fills left to right with 1, 2, 3, then top becomes 1. The right column fills from row 1 down with 4 and 5, then right becomes 1. The bottom row fills right to left with 6 and 7, then bottom becomes 1. The left column fills from row 1 up with 8, then left becomes 1. Now top and left are both 1, so the only cell left is the center, which the next top-row pass fills with 9. The completed grid is:
n^2 cells is written exactly once as the walls sweep inward, so the work scales with the number of cells.n^2 numbers. The four boundary markers and the counter add only a constant amount of extra space.The four passes work, but they need careful bookkeeping, and the two guard checks exist only to avoid rewriting a row or column when the spiral narrows to a single line. There is a way to describe the same walk with one rule for turning instead of four separate passes.
Treat the spiral as one walker that keeps going straight until it has to turn. Instead of four passes, hold a single position and a single heading and step forward one cell at a time. The walker turns only when going straight would run off the grid or step onto a cell that already holds a number.
Store the four clockwise headings as changes in row and column. Right adds 1 to the column, down adds 1 to the row, left subtracts 1 from the column, and up subtracts 1 from the row. When the next step is blocked, rotate to the next heading in the cycle. Because the spiral is a single connected path, this rule alone reproduces the whole pattern.
n by n grid filled with a marker value of 0, meaning unfilled.n^2.n^2 values are placed.Input:
The walker starts at the top-left heading right, placing 1, 2, 3 across the top row. The next step right would leave the grid, so it turns down and places 4, then 5. Stepping down again would leave the grid, so it turns left and places 6, then 7. Stepping left would leave the grid, so it turns up and places 8. Now stepping up would land on the already-filled 1, so it turns right and places 9 in the center. Every cell is filled, giving the same spiral:
n^2 values, and each step does a constant amount of work to check the next cell and possibly turn.n^2 numbers, and it doubles as the record of which cells are filled, so no separate visited structure is needed. The direction arrays use constant space.