[코딩 테스트 RUN] SWEA 수업 : 1954. 달팽이숫자

코테런/SWEA(알고리즘 수업)
2026.03.03

 

 

⚠️ 주의!
SWEA에서 낸 코딩 문제에 대한 해답이 들어있습니다.
열람 시 주의해주세요.

 

사실 프로그래머스에서 푼 것이 있는데, 이건 달팽이 순회 문제이다. 상하좌우를 줄여나가면서 서로의 경계에 가까워지는 형태. 그러나 델타 이동을 이용하는 쪽이 더 낫다. 관련해서는 3월 3일에 풀어서 추가해본다.

 

 

정답

이게 바로 달팽이 순회로 만든 하드 코딩 방식이다.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        for(int test_case = 1; test_case <= T; test_case++) {
            //테스트 케이스 입력
            int N = Integer.parseInt(br.readLine());
            // 번호 N입력

            int[][] arr = new int[N][N];

            int idx = 1;
            int top = 0, bottom = N - 1;
            int left = 0, right = N - 1;

            //달팽이 패턴을 이중배열에 넣어본다..
            while (top <= bottom && left <= right){
                for (int c = left; c <= right; c++) {
                    arr[top][c] = idx;
                    idx++;
                }
                top++;
                for (int r = top; r <= bottom; r++) {
                    arr[r][right] = idx;
                    idx++;
                }
                right--;
                for (int c = right; c >= left; c--) {
                    arr[bottom][c] = idx;
                    idx++;
                }
                bottom--;
                for (int r = bottom; r >= top; r--) {
                    arr[r][left] = idx;
                    idx++;
                }
                left++;
            }

            System.out.printf("#%d%n", test_case);
            for (int r = 0; r < N; r++) {
                for (int c = 0; c < N; c++) {
                    System.out.print(arr[r][c] + " ");
                }
                System.out.println();
            }
        }

    }
}

 

 

이게 바로 델타 이동을 통한 방식이다.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        StringBuilder sb = new StringBuilder();

        for(int test_case = 1; test_case <= T; test_case++){
            int N = Integer.parseInt(br.readLine());
            int num = 1;
            int x = 0, y = 0;
            int[] dx = {0, 1, 0, -1};
            int[] dy = {1, 0, -1, 0};
            int direction = 0;
            int[][] arr = new int[N][N];


            while(num <= N * N){
                arr[x][y] = num++;

                int nx = x + dx[direction];
                int ny = y + dy[direction];

                if(nx < 0 || nx >= N || ny < 0 || ny >= N || arr[nx][ny] != 0){
                    direction = (direction + 1) % 4;
                    nx = x + dx[direction];
                    ny = y + dy[direction];
                }

                x = nx;
                y = ny;

            }

            sb.append("#").append(test_case).append("\n");

            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    sb.append(arr[i][j]).append(" ");
                }
                sb.append("\n");
            }

        }

        System.out.print(sb);
    }
}

 

델타 이동에 대해서는 강의 일지에 적어둔 게 있다.

 

[Java 풀스택 과정 강의] 3월 3일

 

[Java 풀스택 과정 강의] 3월 3일

※ TIL와는 별개로 적는 개인 개발 일지라서 말은 좀 편하게하는 페이지입니다.일지이기 때문에 일기의 성격이 더 강합니다. 사실 알고리즘 강의는 이론 자체보다는 좀 더 실습의 양이 많아서 코

bbbbabbbababababa.tistory.com