[JAVA] 재귀_하노이탑(Level 3)

EunBi Na·2023년 4월 1일
0

P.173
링크텍스트

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

public class Solution {
    private List<int[]> hanoi(int n, int from, int to) {
        if (n == 1) return List.of(new int[] {from, to});
        
        int empty = 6 - from - to;
        
        List<int[]> result = new ArrayList<>();
        result.addAll(hanoi(n-1, from, empty));
        result.addAll(hanoi(1, from, to));
        result.addAll(hanoi(n - 1, empty, to));
        return result;
        
    }
    
    public int[][] solution(int n) {
        return hanoi(n, 1, 3).toArray(new int[0][]);
    }
}
profile
This is a velog that freely records the process I learn.

0개의 댓글