HanoiTower

package demo;

public class HanoiTower {
    public static void moveDish(int level,char from, char inter,char to){
        if(level == 1){
            System.out.println("从" + from + "移动盘子一号到" + to);
        }else {
            moveDish(level - 1,from,to,inter);
            System.out.println("从" + from + "移动盘子" + level + "号到" + to);
            moveDish(level-1,inter,from,to);
        }
    }

    public static void main(String[] args) {
        int level = 3;
        moveDish(level,'A','B','C');
    }
}


/*
*     1.level = 3,
*     movedish(3,a,b,c)
*     movedish(2,a,c,b)--
*     movedish(1,a,b,c) ---- print 1a-c
*                            print 2a-b
*     movedish(1,c,a,b) ---  print 1c-b
*                            print 3a-c
*     movedish(2,b,a,c)
*     movedish(1,b,c,a)----- print 1,b-a
*                            print 2,b-c
*     movedish(1,a,b,c)      print 1,a-c
*
* */

你可能感兴趣的:(JAVA,学习)