|
1 |
| -<<<<<<< HEAD |
2 | 1 | //Tower of Hanoi
|
3 | 2 | //O(2^n)
|
4 | 3 |
|
@@ -57,73 +56,3 @@ public static void main(String[] args) {
|
57 | 56 |
|
58 | 57 |
|
59 | 58 |
|
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