def solution(n):
arr = [[0] * n for i in range(n)]
#2차원 배열 선언하기 많이 나옴. #이거는 종이에 써서 암기.
a = 0
b = 0
x, y = 0,0
moving = [[0,1],[1,0],[0,-1],[-1,0]]
for i in range(1, n**2+1):
arr[x][y] = i
if (x+moving[b][0]) >= n or (y+moving[b][1]) >= n or (y+moving[b][1]) < 0 or (x+moving[b][0]) < 0 or arr[x+moving[b][0]][y+moving[b][1]] != 0:
b = (b + 1)%(len(moving))
x += moving[b][0]
y += moving[b][1]
return arr
def solution(n):
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
y, x = 0, -1
arr = [[0] * n for _ in range(n)]
cnt = 1
direction = 0
while cnt <= n**2:
ny, nx = y + dy[direction], x + dx[direction]
if 0 <= ny < n and 0 <= nx < n and not arr[ny][nx]:
arr[ny][nx] = cnt
cnt += 1
y, x = ny, nx
else:
direction = (direction + 1) % 4
return arr
'Programmers' 카테고리의 다른 글
10진수를 2진수로 바꾸기 bin() 사용. rjust 문자열 정렬 사용하기. (0) | 2024.01.24 |
---|---|
딕셔너리로 알파벳 생성하기, key와 value 위치 바꾸기. 모든 key, value값 가져오기 (1) | 2024.01.23 |
[프로그래머스]유클리드 알고리즘으로 최대공약수 구하기. feat 재귀 (0) | 2024.01.22 |
Dictionary 내부에서 value가 가장 큰 것 찾기! (0) | 2024.01.22 |
[프로그래머스]주사위게임3 value의 최대값으로 key값 구하기 (0) | 2024.01.22 |