상세 컨텐츠

본문 제목

하노이의 탑 (연습문제)

프로그래머스 코딩테스트 풀이

by 발발개발 2022. 8. 1. 17:40

본문

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

 

프로그래머스

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

programmers.co.kr

 

풀이

import java.util.*;

class Solution {
    public static List<int[]> list;

    public int[][] solution(int n) {
        list = new ArrayList<>();

        fn(1, 2, 3, n);

        return list.toArray(new int[0][]);
    }

    public void fn(int start, int mid, int end, int cnt) {
        if (cnt == 1) {
            list.add(new int[]{start, end});
        } else {
            fn(start, end, mid, cnt - 1);
            fn(start, mid, end, 1);
            fn(mid, start, end, cnt - 1);
        }
    }
}

관련글 더보기

댓글 영역