Mock Hanoi Tower by Stacks 227

Question

In the classic problem of 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 from top to bottom (i.e., each disk sits on top of an even larger one). You have the following constraints:

Only one disk can be moved at a time.

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

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

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

Solution

add:如果栈顶元素大于要加入元素,则加入

moveTopTo:如果目标柱子的栈顶元素大于要移动元素,则加入目标柱子栈

moveDisks:如果要移动盘子数量n大于0,则递归地将n-1个盘子通过目标柱子移到buffer柱子,然后将最后一个盘子移到目标柱子,最后再递归地将buffer柱子上的n-1个盘子通过原来的柱子移到目标柱子

代码如下:

public class Tower {
    private Stack<Integer> disks;
    // create three towers (i from 0 to 2)
    public Tower(int i) {
        disks = new Stack<Integer>();
    }

    // Add a disk into this tower
    public void add(int d) {
        if (!disks.isEmpty() && disks.peek() <= d) {
            System.out.println("Error placing disk " + d);
        } else {
            disks.push(d);
        }
    }

    // @param t a tower
    // Move the top disk of this tower to the top of t.
    public void moveTopTo(Tower t) {
        // Write your code here
        if(t.disks.size() != 0 && t.disks.peek() < this.disks.peek()){
            System.out.println("Error Moving");
            return;
        }
        t.disks.push(this.disks.pop());
    }

    // @param n an integer
    // @param destination a tower
    // @param buffer a tower
    // Move n Disks from this tower to destination by buffer tower
    public void moveDisks(int n, Tower destination, Tower buffer) {
        // Write your code here
        if(n == 0){
            return;
        }
        moveDisks(n - 1, buffer, destination);
        moveTopTo(destination);
        buffer.moveDisks(n - 1, destination, this);
    }

    public Stack<Integer> getDisks() {
        return disks;
    }
}
/**
 * Your Tower object will be instantiated and called as such:
 * Tower[] towers = new Tower[3];    
 * for (int i = 0; i < 3; i++) towers[i] = new Tower(i);
 * for (int i = n - 1; i >= 0; i--) towers[0].add(i);    
 * towers[0].moveDisks(n, towers[2], towers[1]);
 * print towers[0], towers[1], towers[2]
*/

results matching ""

    No results matching ""