You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
6
+
7
+
1. Only one disc move at time
8
+
2. Follow order smaller always on the top of bigger
9
+
3. Only top disc must be move
10
+
11
+
Here we considered three poles
12
+
s=source d=destination h=helper
13
+
14
+
i/p: 1
15
+
o/p: Move disc 1 from s to d
16
+
17
+
i/p: 2
18
+
o/p: Move disc 1 from s to h
19
+
Move disc 2 from s to d
20
+
Move disc 1 from h to d
21
+
22
+
i/p: 3
23
+
o/p:Move disc 1 from s to d
24
+
Move disc 2 from s to h
25
+
Move disc 1 from d to h
26
+
Move disc 3 from s to d
27
+
Move disc 1 from h to s
28
+
Move disc 2 from h to d
29
+
Move disc 1 from s to d
30
+
31
+
32
+
No. of move : 2^n-1
33
+
*/
34
+
35
+
importjava.util.Scanner;
36
+
37
+
classtowerOfHanoi{
38
+
staticvoidtoh(chars,chard,charh,intn){
39
+
if(n==1){
40
+
System.out.println("Move disc "+n+" from "+s+" to "+d+"\n");
41
+
return;
42
+
}
43
+
toh(s,h,d,n-1);
44
+
System.out.println("Move disc "+n+" from "+s+" to "+d+"\n");
0 commit comments