Skip to content

Commit 71981c8

Browse files
Changes done.
1 parent e688a64 commit 71981c8

File tree

1 file changed

+0
-71
lines changed

1 file changed

+0
-71
lines changed

Recursion/towerOfHanoi.java

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<<<<<<< HEAD
21
//Tower of Hanoi
32
//O(2^n)
43

@@ -57,73 +56,3 @@ public static void main(String[] args) {
5756

5857

5958

60-
61-
62-
63-
64-
65-
=======
66-
//Tower of Hanoi
67-
//O(2^n)
68-
69-
/*
70-
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:
71-
72-
1. Only one disc move at time
73-
2. Follow order smaller always on the top of bigger
74-
3. Only top disc must be move
75-
76-
Here we considered three poles
77-
s=source d=destination h=helper
78-
79-
i/p: 1
80-
o/p: Move disc 1 from s to d
81-
82-
i/p: 2
83-
o/p: Move disc 1 from s to h
84-
Move disc 2 from s to d
85-
Move disc 1 from h to d
86-
87-
i/p: 3
88-
o/p:Move disc 1 from s to d
89-
Move disc 2 from s to h
90-
Move disc 1 from d to h
91-
Move disc 3 from s to d
92-
Move disc 1 from h to s
93-
Move disc 2 from h to d
94-
Move disc 1 from s to d
95-
96-
97-
No. of move : 2^n-1
98-
*/
99-
100-
import java.util.Scanner;
101-
102-
class towerOfHanoi{
103-
static void toh(char s,char d,char h,int n){
104-
if(n==1){
105-
System.out.println("Move disc "+n+" from "+s+" to "+d+"\n");
106-
return;
107-
}
108-
toh(s,h,d,n-1);
109-
System.out.println("Move disc "+n+" from "+s+" to "+d+"\n");
110-
toh(h,d,s,n-1);
111-
}
112-
public static void main(String[] args) {
113-
System.out.println("Enter no. of discs\n");
114-
Scanner sc = new Scanner(System.in);
115-
int n =sc.nextInt();
116-
sc.close();
117-
toh('s','d','h',n);
118-
119-
}
120-
}
121-
122-
123-
124-
125-
126-
127-
128-
129-
>>>>>>> 326eac95e08c955c209a58ada717425f4ad0d6d7

0 commit comments

Comments
 (0)