KEEP GOING

[python] SWEA 1954 : 달팽이 숫자 본문

code review/implementation

[python] SWEA 1954 : 달팽이 숫자

jmHan 2022. 2. 4. 19:00
반응형

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PobmqAPoDFAUq&categoryId=AV5PobmqAPoDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

1. 코드 구현 

# 동 남 서 북
dx = [0,1,0,-1]
dy = [1,0,-1,0]


def turn(position):
    if position == 3:
        return 0
    return position+1


for t in range(1, T+1):
    N = int(input())
    snail = [[0]*N for _ in range(N)]
    x, y = 0, 0
    snail[x][y] = 1
    p = 0
    for n in range(2, N**2+1):
        nx = x + dx[p]
        ny = y + dy[p]
        if nx<0 or nx>=N or ny<0 or ny>=N or snail[nx][ny] != 0:
            p = turn(p)
            nx = x + dx[p]
            ny = y + dy[p]
        snail[nx][ny] = n

        x = nx
        y = ny
    print(f'#{t}')
    for i in range(N):
        for j in range(N):
            print(snail[i][j], end=' ')
        print()
반응형
Comments