**Q.**In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size form top to bottom (i.e., each disk sites on top of an even larger one). You have the following constraints:

1. Only one disk can be moved at a time.

2. A disk is slid off the top of one tower onto the next tower.

3. A disk can only be placed on top of a larger disk.

Write program to move the disks from the first tower to the last using stacks.

**Algorithm**(For Towers of Hanoi: http://www.codebytes.in/2014/09/towers-of-hanoi-recursive-solution.html )

**Source:**

import java.lang.reflect.Array; class Tower{ Stack<Integer> disks; Tower(int ndisks){ disks = new Stack<>(ndisks); } } class TOH{ int n; Tower[] towers; TOH(int disks, int sourceTower){ n = disks; towers = (Tower[])Array.newInstance(Tower.class, 3); for(int i=0;i<towers.length;++i)towers[i] = new Tower(n); for(int i=disks;i>=1;--i)towers[sourceTower].disks.push(i); } void move(int disks, Tower source, Tower destination, Tower buffer){ if(disks==1)destination.disks.push(source.disks.pop()); else{ move(disks-1, source, buffer, destination); destination.disks.push(source.disks.pop()); move(disks-1, buffer, destination, source); } } void move(int source, int buffer, int destination){ if(towers[source].disks.isEmpty())System.out.println("Tower is empty!"); else move(n, towers[source], towers[destination], towers[buffer]); } } public class Code{ public static void main(String[] args){ TOH toh = new TOH(4, 0); toh.move(0, 1, 2); while(!toh.towers[2].disks.isEmpty()){ System.out.println(toh.towers[2].disks.pop()); } } }

Stack Class

public class Stack <T>{ int top = -1; T[] elements; public Stack(int size){ elements = (T[])new Object[size]; } public int size(){ return top+1; } public void push(T element){ if(top+1 == elements.length){ System.out.println("Overflow."); return; } elements[++top] = element; } public T pop(){ if(top<0){ System.out.println("Underflow."); return null; } return elements[top--]; } public T peek(){ if(top < 0){ System.out.println("Stack is empty!"); return null; } return elements[top]; } public boolean isEmpty(){ return top==-1; } }

**Output:**

1

2

3

4

## No comments:

## Post a Comment