TowerOfHanoi

package ck.test.sort;

public class TowerOfHanoi {

    private int size;
    private String fromPeg;
    private String toPeg;
    private String usingPeg;

    public TowerOfHanoi(int size, String fromPeg,
            String toPeg, String usingPeg) {
    this.size = size;
    this.fromPeg = fromPeg;
    this.toPeg = toPeg;
    this.usingPeg = usingPeg;
    }

    public void solveTower() {
        // Print out some header information.
        System.out.println(“Steps for solving ” + size +
                   ” disk tower of hanoi puzzle, “);
        System.out.println(“starting with discs on ” + fromPeg +
                   ” and moving discs to ” + toPeg + “:”);
        System.out.println();
   
        // Solve the problem by moving size disks from the
        // fromPeg to the toPeg using the usingPeg.
        solveTower(size, fromPeg, toPeg, usingPeg);// C2
    }

    // Recursive helper method that prints the instructions for
    // moving numDisks from the fromPeg to the toPeg using the
    // usingPeg.
    private void solveTower(int num, String from, String to, String using) {
        if (num == 1) {
            // Base Case: Move 1 disk…
            System.out.println(“Move disk from ” + from +
                       ” to ” + to + “.”);
        }
        else {
            // Recursive Case:
            //   Move num-1 disks from the from peg to
            //   the usingPeg using the toPeg.
            solveTower(num-1, from, using, to);   // RC1
   
            //   Move 1 disk from the fromPeg to the toPeg using
            //   the usingPeg.
            solveTower(1, from, to, using);            // RC2
           
            //   Move num-1 disks from the usingPeg to
            //   the toPeg using the fromPeg.
            solveTower(num-1, using, to, from);   // RC3
        }
    }

    public static void main(String[] args) {
        TowerOfHanoi tower = new TowerOfHanoi(4,”A”, “B”, “C”);
        tower.solveTower();// C1
        //ADD STACK TO THE THREE POLE SO THAT THE PROCESS CAN BE BACK-TRACABLE…
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s