231004 하노이의 탑

Jongleee·2023년 10월 4일
0

TIL

목록 보기
381/737
 public int[][] solution(int n) {
	List<int[]> moveList = generateMoves(n, 1, 3, 2);
	int[][] answer = new int[moveList.size()][];

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

	return answer;
}

private List<int[]> generateMoves(int n, int start, int to, int mid) {
	List<int[]> moveList = new ArrayList<>();

	if (n == 1) {
		moveList.add(new int[] { start, to });
	} else {
		moveList.addAll(generateMoves(n - 1, start, mid, to));
		moveList.add(new int[] { start, to });
		moveList.addAll(generateMoves(n - 1, mid, to, start));
	}

	return moveList;
}

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

0개의 댓글