CTCI 1, 3.4

Source: Internet
Author: User

In the classic problem of the towers of Hanoi, you have 3 towers and ndisks of different sizes which can slide onto any tower. the puzzle starts with disks sorted in ascending order of size from top to bottom (I. E ., each disk sits 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 a program to move the disks from the first tower to the last using stacks.

Use recursion to implement the algorithm. assume we are moving from A to C, with an additional B Tower. if there is only one plate, directly move it from A to C, Else, we first move the n-1, which n is the total plates on a from A to B using C as buffer, then move the last nth plate from A to C, then move the rest n-1 plates from B to C using a as buffer. in my implement, I use a inner class "Tower ". However, when instantiation a tower VaR in a static call, We need to first instantiation the outer class!

/* Using the recursion to implement the algorithm*/import java.util.*;public class Hanoi {    public void recursehanoi(int n, Tower a, Tower b, Tower c) {        if(n == 1) {             System.out.println("Move the " + a.peek() + "th plate from " + a.name + " to " + c.name);            c.add(a.remove());         }        else {            //Move the top n-1 plates from a to b using c as buffer            recursehanoi(n-1, a, c, b);            //Move the last n from a to c            System.out.println("Move the " + a.peek() + "th plate from " + a.name + " to " + c.name);            c.add(a.remove());             //Move the rest n-1 plates from b to c using a as buffer            recursehanoi(n-1, b, a, c);        }    }    public class Tower {        public String name;        public Stack<Integer> plates = new Stack<Integer>();        public Tower(String name) {            this.name = name;        }        public void add(int plate) {            plates.push(plate);        }        public int peek() {            return plates.peek();        }        public int remove() {            return plates.pop();        }    }    public static void main(String[] args) {        Hanoi ha = new Hanoi();        Hanoi.Tower a = new Hanoi().new Tower("a"); a.add(3); a.add(2); a.add(1);        Hanoi.Tower b = new Hanoi().new Tower("b"); Hanoi.Tower c = new Hanoi().new Tower("c");        ha.recursehanoi(3, a, b, c);    }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.