T = int(input())
di = [0, 1, 0, -1]
dj = [1, 0, -1, 0]
for t in range(1, T+1):
n = int(input())
lst = [[0 for _ in range(n+2)] for _ in range(n+2)]
for i in range(1, n+2):
lst[0][i] = -1
lst[n + 1][i] = -1
lst[i][0] = -1
lst[i][n + 1] = -1
i = 1
j = 0
num = 1
d = 0
while num <= n*n:
next_i = i + di[d]
next_j = j + dj[d]
if lst[next_i][next_j]:
d = (d + 1) % 4
continue
i = next_i
j = next_j
lst[i][j] = num
num += 1
print('#{}'.format(t))
for i in range(1, n+1):
print(*lst[i][1:n+1], sep=' ')
'알고리즘 문제 풀이 > SWEA' 카테고리의 다른 글
[SWEA 2001 python] 파리 퇴치 (0) | 2021.08.13 |
---|---|
[SWEA 1209 python] Sum (0) | 2021.08.13 |
[SWEA 1945 python] 간단한 소인수분해 (0) | 2021.08.13 |
[SWEA 1208 python] Flatten (0) | 2021.08.13 |
[SWEA 5789 python] 현주의 상자 바꾸기 (0) | 2021.08.13 |