forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTowerOfHanoi.java
More file actions
39 lines (34 loc) · 1.28 KB
/
TowerOfHanoi.java
File metadata and controls
39 lines (34 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// https://en.wikipedia.org/wiki/Tower_of_Hanoi
/**
* TowerOfHanoi.java
*
* This program demonstrates the recursive solution for the Tower of Hanoi problem.
* It prints the sequence of moves required to move n disks from source rod to target rod.
*/
public class TowerOfHanoi {
/**
* Solves the Tower of Hanoi problem recursively.
*
* @param n Number of disks
* @param from Source rod
* @param to Target rod
* @param aux Auxiliary rod
*/
public static void solveTowerOfHanoi(int n, char from, char to, char aux) {
if (n == 1) {
System.out.println("Move disk 1 from " + from + " to " + to);
return;
}
// Move n-1 disks from source to auxiliary
solveTowerOfHanoi(n - 1, from, aux, to);
// Move the nth disk from source to target
System.out.println("Move disk " + n + " from " + from + " to " + to);
// Move the n-1 disks from auxiliary to target
solveTowerOfHanoi(n - 1, aux, to, from);
}
public static void main(String[] args) {
int numberOfDisks = 3; // You can change this to test with more disks
System.out.println("Tower of Hanoi solution for " + numberOfDisks + " disks:");
solveTowerOfHanoi(numberOfDisks, 'A', 'C', 'B');
}
}