여름의 서재

[SWEA] 1954_달팽이 본문

알고리즘/SWEA

[SWEA] 1954_달팽이

엉아_ 2021. 8. 13. 00:33
728x90
T = int(input())
for tc in range(T):
    N = int(input())

    matrix = [[0]*N for i in range(N)]
    dx = [0, 1, 0, -1]
    dy = [1, 0, -1, 0]

    cnt = 1
    i, j = 0, -1
    k = 0
    while cnt <= N*N:
        x, y = i+dx[k], j+dy[k]
        if x < N and y < N and matrix[x][y] == 0:
            matrix[x][y] = cnt
            cnt += 1
            i, j = x, y
        else:
            k = (k+1) % 4

    print('#{0}'.format(tc+1))
    for i in matrix:
        print(*i)

'알고리즘 > SWEA' 카테고리의 다른 글

[SWEA] 1221_GNS  (0) 2021.08.17
[SWEA] 1210_Ladder1  (0) 2021.08.13
[SWEA] 2001_파리 퇴치  (0) 2021.08.13
[SWEA] 4836_색칠하기  (0) 2021.08.13
[SWEA] 4837_부분집합의 합  (0) 2021.08.13
Comments