t = int(input())
for tc in range(1, t+1):
n = int(input())
space = [[0]*n for _ in range(n)]
#위, 오른쪽, 아래, 왼쪽으로 움직이는 방향전환
dx = [0,1,0,-1]
dy = [1,0,-1,0]
direction = 0
#초기 좌표
x = y = 0
#space[0][0] = 1 부터 돌면서 진행하기 위함
space[x][y] = 1
for num in range(2, n*n+1):
x += dx[direction]
y += dy[direction]
#2부터 숫자 넣기.
space[x][y] = num
#범위를 확인해주는 작업 범위를 넘어가지 않으면 계속해서 같은 방향성을 가지고 달팽이가 움직인다.
if 0 <= x + dx[direction] < n and 0 <= y + dy[direction] < n and not space[x+dx[direction]][y+dy[direction]]:
continue
#설정 범위를 벗어난다면 dx dy의 방향을 다음으로 바꿔줌
if direction != 3:
direction += 1
else:
direction = 0
print(f'#{tc}')
for i in space:
print(*i)
'문제풀이 > SW Expert Academy' 카테고리의 다른 글
[SWEA][D2] 1946. 간단한 압축 풀기 (파이썬/Python) (0) | 2021.10.08 |
---|---|
[SWEA][D2] 1948. 날짜 계산기 (파이썬/Python) (0) | 2021.09.24 |
[SWEA][D2] 1288. 새로운 불면증 치료법 (파이썬/Python) (0) | 2021.09.17 |
[SWEA][D2] 1204. [S/W 문제해결 기본] 1일차 - 최빈수 구하기 (파이썬/Python) (0) | 2021.09.17 |
[SWEA][D2] 1284. 수도 요금 경쟁 (파이썬/Python) (0) | 2021.09.17 |