Οι πύργοι του Ανόι

import java.util.*;

public class Hanoi {
        static Stack A, B, C;

        /** Display the contents of a collection with a given name */
        static void showCollection(String name, Collection c) {
                Iterator i;

                System.out.print(name + ": ");
                for (i = c.iterator(); i.hasNext(); )
                        System.out.print(i.next() + " ");
                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 void move(int n, Stack from, Stack to, Stack 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 = 4;
                A = new Stack();
                B = new Stack();
                C = new Stack();

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