分治实现汉诺塔问题

package dataStruct.常用算法;

public class 分治法解决汉诺塔 {
    public static void main(String[] args) {
        hanuota(3,'A','B','C');
    }
    public static void hanuota(int num,char a,char b, char c){
        //当只有一个盘子时,直接将盘子冲a移动到c
        if(num == 1){
            System.out.println("第"+num+"个盘子从 "+a+"移动到"+c);
        }
        //如果有两个以上的盘子,将上面的所有盘子看称一个整体,将上面所有的盘子移动到b盘,再将最下面的的盘子移动到c盘
        else
            {
                hanuota(num - 1, a, c, b);
                System.out.println("第" + num + "个盘子从" + a + "移动到" + c);
                hanuota(num - 1, b, a, c);
            }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_52655865/article/details/122456041