230505 하노이의 탑

Jongleee·2023년 5월 5일
0

TIL

목록 보기
251/737
public static int[][] solution(int n) {
    List<int[]> moveList = new ArrayList<>();
    generateMoves(n, 1, 3, 2, moveList);

    int[][] answer = new int[moveList.size()][];
    for(int i = 0; i < moveList.size(); i++){
        answer[i] = moveList.get(i);
    }
    return answer;
}

private static void generateMoves(int n, int start, int to, int mid, List<int[]> moveList) {
    if(n == 1){
        moveList.add(new int[]{start, to});
        return;
    }
    generateMoves(n-1, start, mid, to, moveList);
    moveList.add(new int[]{start, to});
    generateMoves(n-1, mid, to, start, moveList);
}

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

0개의 댓글