Παράδειγμα: οι πύργοι του Ανόι

import java.util.Stack;
import java.util.Iterator;
import java.util.Collection;

public class Hanoi {
    static Stack<Integer> A, B, C;

    /** Display the contents of a collection with a given name */
    static <E> void showCollection(String name, Collection <E> c) {
        System.out.print(name + ": ");
        for (E e : c)
            System.out.print(e + " ");
        System.out.println();
    }

    /** Display the hanoi towers */
    static void showConfiguration() {
        showCollection("A", A);
        showCollection("B", B);
        showCollection("C", C);
        System.out.println();
    }

    /** Move n blocks from to using tmp */
    static <E> void move(int n, Stack<E> from, Stack<E> to, Stack<E> tmp) {
        if (n == 1) {
            to.push(from.pop());
            showConfiguration();
        } else {
            move(n - 1, from, tmp, to);
            to.push(from.pop());
            showConfiguration();
            move(n - 1, tmp, to, from);
        }
    }

    public static void main(String args[]) {
        final int N = 64;
        A = new Stack<Integer>();
        B = new Stack<Integer>();
        C = new Stack<Integer>();

        for (int i = N; i > 0; i--)
            A.push(i);
        showConfiguration();
        move(N, A, C, B);
    }
}