|
| 1 | +<<<<<<< HEAD |
| 2 | +//Tower of Hanoi |
| 3 | +//O(2^n) |
| 4 | + |
| 5 | +/* |
| 6 | +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: |
| 7 | +
|
| 8 | +1. Only one disc move at time |
| 9 | +2. Follow order smaller always on the top of bigger |
| 10 | +3. Only top disc must be move |
| 11 | +
|
| 12 | +Here we considered three poles |
| 13 | +s=source d=destination h=helper |
| 14 | +
|
| 15 | +i/p: 1 |
| 16 | +o/p: Move disc 1 from s to d |
| 17 | +
|
| 18 | +i/p: 2 |
| 19 | +o/p: Move disc 1 from s to h |
| 20 | + Move disc 2 from s to d |
| 21 | + Move disc 1 from h to d |
| 22 | +
|
| 23 | +i/p: 3 |
| 24 | +o/p:Move disc 1 from s to d |
| 25 | + Move disc 2 from s to h |
| 26 | + Move disc 1 from d to h |
| 27 | + Move disc 3 from s to d |
| 28 | + Move disc 1 from h to s |
| 29 | + Move disc 2 from h to d |
| 30 | + Move disc 1 from s to d |
| 31 | +
|
| 32 | +
|
| 33 | +No. of move : 2^n-1 |
| 34 | +*/ |
| 35 | + |
| 36 | +import java.util.Scanner; |
| 37 | + |
| 38 | +class towerOfHanoi{ |
| 39 | + static void toh(char s,char d,char h,int n){ |
| 40 | + if(n==1){ |
| 41 | + System.out.println("Move disc "+n+" from "+s+" to "+d+"\n"); |
| 42 | + return; |
| 43 | + } |
| 44 | + toh(s,h,d,n-1); |
| 45 | + System.out.println("Move disc "+n+" from "+s+" to "+d+"\n"); |
| 46 | + toh(h,d,s,n-1); |
| 47 | +} |
| 48 | +public static void main(String[] args) { |
| 49 | + System.out.println("Enter no. of discs\n"); |
| 50 | + Scanner sc = new Scanner(System.in); |
| 51 | + int n =sc.nextInt(); |
| 52 | + sc.close(); |
| 53 | + toh('s','d','h',n); |
| 54 | + |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | + |
| 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