-
[프로그래머스] 정수를 나선형으로 배치하기알고리즘 노트 2023. 12. 12. 10:42
주어진 행과 열의 수 만큼의 영역에서 나선형을 돌며 1씩 증가하면 됩니다.
위 내용으로 유추해볼 때 나선형의 특정 영역에서 연산이 처리되어야 할 겁니다.
아래 그림을 먼저 보시죠.
배열의 인덱스를 x와 y라고 지칭했을때, 첫번째 이동 경로인 1에서 2는 x는 그대로 두고 y가 증가합니다.
즉, [0, 0], [0, 1], [0, 2], [0, 3], [0, 4] 순으로 y가 증가하죠.
그리고 두번째 이동 경로인 2에서 3은 y는 그대로 두고 x가 증가합니다.
즉, [1, 4], [2, 4], [3, 4], [4, 4] 순으로 x가 증가하죠.
3에서 4는 1에서 2의 역순으로 y가 줄어들고, 4에서 1은 2에서 3의 역순으로 x가 감소합니다.
코드를 보면 다음과 같습니다.
n을 회전 기점으로 사용하기 때문에 지속적으로 -- 처리를 해줍니다.
'알고리즘 노트' 카테고리의 다른 글
[프로그래머스] 정사각형으로 만들기 (0) 2023.12.13 [프로그래머스] 배열의 원소만큼 추가하기 (0) 2023.11.03 [프로그래머스] 5명씩 (0) 2023.10.25 [프로그래머스] 왼쪽 오른쪽 (0) 2023.10.24 [프로그래머스] 배열 조각하기 (0) 2023.10.23