-
Notifications
You must be signed in to change notification settings - Fork 368
/
TowerOfHanoi.java
65 lines (47 loc) · 2.11 KB
/
TowerOfHanoi.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
The Tower of Hanoi is a problem that involves moving n disks from one tower to another,
following specific rules.
It consists of three towers, usually labeled A, B, and C.
The puzzle starts with a number of disks of different sizes arranged in increasing order from top to bottom
(i.e. smallest on top and then so on...) on one tower, typically tower A.
The objective is to move the entire stack of disks to another tower, following these rules:
Only one disk can be moved at a time.
Each move involves taking the top disk from one tower and placing it onto another tower.
The larger disk should never be placed on top of a smaller disk. The disks must always be arranged
in decreasing order from bottom to top on any tower.
Algorithm: We will be using Recursion to solve this problem
S1: First we move top (n-1) disks from source(i.e. the starting tower) to helper(i.e. the middle tower)
S2: Now we are left with one disk at source which we will transfer directly to the destination(i.e. the last tower)
S3: Now we will move the (n-1) disks at the helper to the destination tower
*/
import java.util.*;
public class TowerOfHanoi
{
Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
TowerOfHanoi toh = new TowerOfHanoi();
toh.display();
}
public void display()
{
System.out.println("Enter the number of disks");
int num_disks = sc.nextInt();
process(num_disks,"A","B","C");
// A, B, C are the names of the towers, starting from A and delivering all the disks to C
}
public void process(int n, String src, String helper, String dest)
{
if(n == 1) // base case
{
System.out.println("Transfer disk " + n + " from " + src + " to " + dest);
return;
}
process(n-1,src,dest,helper);
//moving the top (n-1) disks from A to B
System.out.println("Transfer disk " + n + " from " + src + " to " + dest);
//moving the remaining one disk from A to C
process(n-1,helper,src,dest);
//now moving the (n-1) disks on B to C
}
}