forked from Sheb1995/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTowerOfHanoiExample.java
More file actions
31 lines (31 loc) · 1.33 KB
/
TowerOfHanoiExample.java
File metadata and controls
31 lines (31 loc) · 1.33 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
public class TowerOfHanoiExample
{
// A recursive method to find the solution of the puzzle, called Tower of Hanoi
static void twrOfHanoi(int disk, char fromPole, char toPole, char auxPole)
{
// handling the base case
if (disk == 1)
{
System.out.println("Moving disk 1 from pole " + fromPole + " to pole " + toPole);
return;
}
// The first recursive call
// recursively moving the n - 1 disk from the source pole to the auxiliary pole
twrOfHanoi(disk - 1, fromPole, auxPole, toPole);
// move the nth pole from the source pole to the destination pole
System.out.println("Moving disk " + disk + " from pole " + fromPole + " to pole " + toPole);
// The second recursive call
// recursively moving the n - 1 disk from the auxiliary pole to the destination pole
twrOfHanoi(disk - 1, auxPole, toPole, fromPole);
}
// The driver method
public static void main(String argvs[])
{
int disks = 3; // total number of disks
char firstPole = 'A'; // first pole
char secondPole = 'B'; // second pole
char thirdPole = 'C'; // third pole
// invoking the method twrOfHanoi
twrOfHanoi( disks, firstPole, thirdPole, secondPole );
}
}