본문 바로가기
카테고리 없음

[Java] 프로그래머스 - 하노이의 탑

by C0MPAS 2024. 1. 22.

1월 22일(월) - 5장 재귀

 

프로그래머스

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

programmers.co.kr


import java.util.ArrayList;
import java.util.List;

public class Solution {

    private void hanoi(int n, int from, int to, List<int[]> process){
        if(n == 1)
        {
            process.add(new int[] {from, to});
            return;
        }

        int empty = 6-from-to;

        hanoi(n-1, from, empty, process);
        hanoi(1, from, to, process);
        hanoi(n-1, empty, to, process);
    }
    public int[][] solution(int n){
        List<int[]> process = new ArrayList<>();
        hanoi(n, 1, 3, process);

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

 

댓글