public class HanoiTower {
// 재귀 메서드: 하노이의 탑 이동 수행
public static void hanoi(int n, char start, char end, char another) {
if (n == 1) { // 종료 조건: 원반이 1개일 때
System.out.println("Move 1 from " + start + " to " + end);
return;
}
hanoi(n - 1, start, another, end);
System.out.println("Move " + n + " from " + start + " to " + end);
hanoi(n - 1, another, end, start);
}
public static void main(String[] args) {
int numDisks = 3;
hanoi(numDisks, 'A', 'C', 'B');
}
}
'Data Structures & Algorithms' 카테고리의 다른 글
Merge Two Sorted Arrays (0) | 2025.03.08 |
---|---|
RBST: Convert a Sorted Linked List to a Height-Balanced BST (0) | 2025.03.04 |
Priority Queue (Heap): Implement a Task Scheduler (Comparable & Comparator) (0) | 2025.02.27 |
Graph: Check if a Path Exists in an Undirected Graph (0) | 2025.02.26 |
Set: Remove Duplicates (0) | 2025.02.25 |