A Spiral Walk
InterviewTime limit1sMemory limit128 MB
Fill an N by N grid with the visit order of a clockwise spiral that starts at the top-left and ends near the center.
- Level
Easy3 of 10
- Topics
- Simulation, Matrix, Implementation
- Solved
- No attempts yet
Problem
The cows love to walk in their square pasture, which has sides of length () and is partitioned into unit squares.
Bessie has planned the longest possible walk that starts at the upper-left square and ends at the center of the pasture (or near the center when is even), passing through every square exactly once after starting.
She has chosen a clockwise spiral route (illustrated below). Write a program that prints a map showing the order in which she visits each square.
For example, for pastures of size and , the visiting orders are:
1 2 3 1 2 3 4
8 9 4 12 13 14 5
7 6 5 11 16 15 6
10 9 8 7
Input
The first line contains a single integer .
Output
Print lines, each containing space-separated integers. Each integer is the order in which the corresponding square is visited.