-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTowerOfHanoi.java
More file actions
30 lines (25 loc) · 798 Bytes
/
TowerOfHanoi.java
File metadata and controls
30 lines (25 loc) · 798 Bytes
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
package recursion;
/**
* It takes exponential time.
* For n discs, number of moves are (2^n)-1
* Number of moves - [1->1, 2->3, 3->7, 4->15, 5->31...]
*/
public class TowerOfHanoi {
private static int numOfMoves = 0;
public void move(int numberOfDiscs, char from, char to, char inter) {
if (numberOfDiscs == 1) {
System.out.println("Moving disc 1 from " + from + " to " + to);
numOfMoves++;
} else {
move(numberOfDiscs - 1, from, inter, to);
System.out.println("Moving disc " + numberOfDiscs + " from " + from + " to " + to);
numOfMoves++;
move(numberOfDiscs - 1, inter, to, from);
}
}
public static void main(String[] args) {
TowerOfHanoi toh = new TowerOfHanoi();
toh.move(5, 'A', 'C', 'B');
System.out.println("Number of moves: " + numOfMoves);
}
}