코딩테스트/SWExpertAcademy
[Python] SWEA 13704 - 달팽이숫자
Ssubini
2022. 4. 13. 22:36
[2022.02.15]
- 반복문 활용해서 달팽이 모양으로 숫자 나열하기
T = int(input())
for tc in range(T):
N = int(input())
num = 1
answer = [[0]*N for _ in range(N)]
check = 0
maxi = N
maxj = N
mini = 0
minj = -1
i = 0
j = 0
while num <= N*N:
if check % 4 == 0:
answer[i][j] = num
num += 1
j += 1
if j == maxj :
check += 1
j -= 1
maxj -= 1
i += 1
continue
continue
elif check % 4 == 1:
answer[i][j] = num
num += 1
i += 1
if i == maxi :
check += 1
i -= 1
maxi -= 1
j -= 1
continue
continue
elif check % 4 == 2:
answer[i][j] = num
num += 1
j -= 1
if j == minj :
check += 1
j += 1
minj += 1
i -= 1
continue
continue
elif check % 4 == 3:
answer[i][j] = num
num += 1
i -= 1
if i == mini :
check += 1
i += 1
mini += 1
j += 1
continue
continue
print(f'#{tc+1}')
for i in range(N):
print(*answer[i])
=> 숫자가 올 수 있는 끝점과 방향을 반복문 내부의 조건으로 설정하여 풀기.