Tuesday, April 2, 2013

Java: Towers Of Hanoi

/*
Towers Of Hanoi.
There are three pegs: A, B and C. There are n disks. All disks are different in size.
The disks are initially stacked on peg A so that they increase in size from the top to the bottom.
The goal is to transfer the entire tower from the A peg to the C peg.
One disk at a time can be moved from the top of a stack either to an empty peg or to
a peg with a larger disk than itself on the top of its stack.

The method should return a sequence of disk moves, each move is a String with two letters (A, B or C)
corresponding to the peg the disk moves from and the peg it moves to.
For example, the move "AC" means that a top disk from peg A should be moved to peg C.
*/

  1. import java.util.ArrayList;

  2. importjava.util.List;

  3.  

  4.  

  5. publicclassTowersOfHanoi {

  6.  

  7.     privatestaticList<String> list= newArrayList<String>();

  8.     staticString a="A";

  9.     staticString c="C";

  10.     staticString b="B";

  11.     staticString s="";

  12.     publicstaticvoidmove(intn, intstartPole, intendPole) {

  13.         if(n== 0){

  14.           return;

  15.         }

  16.         intintermediatePole = 6- startPole - endPole;

  17.         move(n-1, startPole, intermediatePole);

  18.         System.out.println("Move "+n + " from "+ startPole + " to "+endPole);

  19.         if(startPole==1)

  20.             s=s+a;

  21.         if(startPole==2)

  22.             s=s+b;

  23.         if(startPole==3)

  24.             s=s+c;

  25.         if(endPole==1)

  26.             s=s+a;

  27.          

  28.         if(endPole==2)

  29.             s=s+b;

  30.         if(endPole==3)

  31.             s=s+c;

  32.          

  33.         list.add(s);

  34.         s="";

  35.         move(n-1, intermediatePole, endPole);

  36.       }

  37.        

  38.     publicstaticList<String> transferFromAtoC(intn) {

  39.         /*

  40.           Towers Of Hanoi.

  41.           There are three pegs: A, B and C. There are n disks. All disks are different in size.

  42.           The disks are initially stacked on peg A so that they increase in size from the top to the bottom.

  43.           The goal is to transfer the entire tower from the A peg to the C peg.

  44.           One disk at a time can be moved from the top of a stack either to an empty peg or to

  45.           a peg with a larger disk than itself on the top of its stack.

  46.  

  47.           The method should return a sequence of disk moves, each move is a String with two letters (A, B or C)

  48.           corresponding to the peg the disk moves from and the peg it moves to.

  49.           For example, the move "AC" means that a top disk from peg A should be moved to peg C.

  50.          */

  51.          

  52.         move(n, 1, 3);

  53.         returnlist;

  54.     }

  55.       publicstaticvoidmain(String[] args) {

  56.           List l1 = newArrayList();

  57.         transferFromAtoC(1);

  58.         System.out.println(list);

  59.       }

  60. }


Download Link: Click Here

0 comments: