Given a positive and odd integer n, return an n x n grid filled with integers from 0 to n^2 - 1 in spiral order.
Rules:
- Start at the center cell and place
0. - Move down first, then continue turning clockwise.
- Keep placing the next number whenever you step into a valid cell.
Illustration (n = 5):
Example:
- Input:
n = 3 - Output:
[[4,5,6],[3,0,7],[2,1,8]]
| 4 | 5 | 6 |
| 3 | 0 | 7 |
| 2 | 1 | 8 |
- Explanation:
- Start at center (1,1):
0 - Move Down: (2,1) ->
1 - Move Left: (2,0) ->
2 - Move Up: (1,0) ->
3, (0,0) ->4 - Move Right: (0,1) ->
5, (0,2) ->6 - Move Down: (1,2) ->
7, (2,2) ->8
- Start at center (1,1):
-
-
-
-
0
-
-
-
-
r=1
c=1
curr=1
steps=1
Step 1 / 6
Step 1:
Start at the center (1,1) with value 0.
Focus: select @ [4]
r=1c=1curr=1steps=1