# -*- coding: utf-8 -*- """ No.401 数字の渦巻き https://yukicoder.me/problems/no/401 """ import sys from sys import stdin from itertools import cycle input = stdin.readline def next_repeat(n): """ 渦巻きを書く際のある方向への移動回数。 最初は右方向にnマス連続で移動する。次は下方向にn-1マス移動する。その次は左方向にn-1マス移動する。 これ以降は2回方向が変わるごとに移動量が-1されていく。 """ yield n while True: n -= 1 if n < 0: raise ValueError yield n yield n def next_pos(n): """ 次に数字を埋める渦巻きのx, y座標を返す """ x, y = -1, 0 # 次に数字を塗る座標。最初に移動した後に0, 0になるようにx=-1からスタート d = cycle([(1, 0), (0, 1), (-1, 0), (0, -1)]) # 渦巻きを書く際の方向、右・下・左・上の順で x, yの変化量 c = next_repeat(n) # 渦巻きの一辺の長さ while True: dx, dy = d.__next__() count = c.__next__() for _ in range(count): nx = x + dx ny = y + dy yield nx, ny x = nx y = ny def guruguru(N): """ 数字の渦巻きをつくる :param N: 一辺のサイズ :return: 完成した渦巻 """ ans = [[''] * N for _ in range(N)] p = next_pos(N) for i in range(1, N*N+1): x, y = p.__next__() ans[y][x] = '{:03d}'.format(i) # そのままプリントできるように0パディングした文字列で塗る return ans def main(args): N = int(input()) ans = guruguru(N) for row in ans: print(*row, sep=' ') if __name__ == '__main__': main(sys.argv[1:])