본문 바로가기

Algorithm/Practice

Programmers - 삼각 달팽이 with JAVA

 

문제

반시계방향으로 1층부터 n층까지 채워 피라미드를 만든다.

 

 

 

로직

- 재귀함수를 사용한다.

- 아래로 가는경우(x+1), 위로가는 경우(x-1, y-1), 옆으로 가는 경우(y+1) 로 나누어 차례대로 돌린다.

 

 

코드1

import java.util.*;
class Solution {
    public List<Integer> solution(int n) {
        List<Integer> answer = new ArrayList<>();
        int[][] map = new int[n][n];
        find(0, 0, 0, 1, n, map);        
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                if(map[i][j] == 0){
                    break;
                }
                answer.add(map[i][j]);
            }
        }
        return answer;
    }
    public static void find(int direction, int now_x, 
                            int now_y, int value, int count, int[][] map){
        if(count == 1){
            map[now_x][now_y] = value;
            return;
        }
        if(direction == 0){
            for(int x=now_x; x<now_x+count; x++){
                map[x][now_y] = value;
                value += 1;
            }
            find(1, now_x+count-1, now_y+1, value, count-1, map);
        }
        if(direction == 1){
            for(int y=now_y; y<now_y+count; y++){
                map[now_x][y] = value;
                value += 1;
            }
            find(2, now_x-1, now_y+count-2, value, count-1, map);
        }
        if(direction == 2){
            for(int x=now_x; x>now_x-count; x--){
                map[x][now_y] = value;
                value += 1;
                now_y -= 1;
            }
            find(0, now_x-count+2, now_y+1, value, count-1, map);
        }
    }
}

 

 

코드 2

import java.util.*;
class Solution {
    public static int value = 1;
    public static int x = -1;
    public static int y = 0;
    public static int[][] map = new int[1001][1001];
    public List<Integer> solution(int n) {
        List<Integer> answer = new ArrayList<>();
        int direction = 0;
        for(int count=n; count>0; count--){
            check(direction, count);
            direction += 1;
            if(direction >= 3){
                direction = 0;
            }
        }
        for(int x=0; x<n; x++){
            for(int y=0; y<n; y++){
                if(map[x][y] != 0){
                    answer.add(map[x][y]);
                }
            }
        }
        return answer;
    }
    public static void check(int direction, int count){
        if(direction == 0){
            down(count);
        }
        if(direction == 1){
            right(count);
        }
        if(direction == 2){
            up(count);
        }
    }
    public static void down(int count){
        for(int i=0; i<count; i++){
            x += 1;
            map[x][y] = value;
            value += 1;    
        }
    }
    public static void right(int count){
        for(int i=0; i<count; i++){
            y += 1;
            map[x][y] = value;
            value += 1;    
        }
    }
    public static void up(int count){
        for(int i=0; i<count; i++){
            x -= 1;
            y -= 1;
            map[x][y] = value;
            value += 1;
        }
    }
}