递归汉诺塔

递归汉诺塔

public class HanoiQuestion {
    private static void move(char from, char to) {
        System.out.println("move the top plate from " + from + " to " + to);
    }

    public static void hanoi(char from, char to, char mid, int index) {
        if (index == 1)
            move(from, to);
        else {
            hanoi(from, mid, to, index - 1);
            move(from, to);
            hanoi(mid, to, from, index - 1);
        }
    }

    public static void main(String[] args) {
        HanoiQuestion.hanoi('A', 'B', 'C', 3);
    }
}

猜你喜欢

转载自dugu108.iteye.com/blog/1771213