헝D의 일기장

https://school.programmers.co.kr/learn/courses/30/lessons/12946

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

나의풀이

import java.util.*;
class Solution {
    public static int[][] answer;
    public static int index=0;
    public int[][] solution(int n) {
        answer = new int[(int)Math.pow(2,n)-1][2];

        recur(n, 1, 3, 2);//막대 1에서 막대 2를 거쳐 막대 3으로 옮긴다.
        
        return answer;
    }
    
    public static void recur(int n, int start, int to, int mid){
        if(n==1){
            answer[index++]=new int[]{start, to};
        }else{
            //n개중 n-1개를 1에서 2로 옮긴다
            recur(n-1, start, mid, to);

            //1번의 가장 큰 n을 1에서 3으로 옮긴다. 
            answer[index++] = new int[]{start, to};

            //2번의 n-1개를 2에서 3으로 옮긴다
            recur(n-1, mid, to, start);
        
        }
    }
}
profile

헝D의 일기장

@헝D

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!