package offer;
/**
* 需求: 汉若塔问题,就是把A柱子上面从大到小一次叠放的盘子借助B柱移到C柱上去,规则是一次只能移动一个盘子,大盘子不能放到小盘子之上
* 思路:递归
* 1.先将A上面的n-1个盘子,移到B柱子上
* 2.然后把A上最大的一个盘子放到C上
* 3、然后把B上面的n-1个盘子移到A上
* @author lpc
*/
public class Hannou {
public static void moveDish(int level, char from, char inter, char to){
if(level == 1){
System.out.println("从" + from + "移动盘" + level +"到" + to);
}else{
moveDish(level - 1, from, to, inter);
System.out.println("从" + from + "移动盘" + level +"到" + to);
moveDish(level -1, inter, from, to);
}
}
/**
*
* @param args
*/
public static void main(String[] args) {
int nDisk = 5;
moveDish(nDisk, 'A', 'B', 'C');
}
}
/**
* 需求: 汉若塔问题,就是把A柱子上面从大到小一次叠放的盘子借助B柱移到C柱上去,规则是一次只能移动一个盘子,大盘子不能放到小盘子之上
* 思路:递归
* 1.先将A上面的n-1个盘子,移到B柱子上
* 2.然后把A上最大的一个盘子放到C上
* 3、然后把B上面的n-1个盘子移到A上
* @author lpc
*/
public class Hannou {
public static void moveDish(int level, char from, char inter, char to){
if(level == 1){
System.out.println("从" + from + "移动盘" + level +"到" + to);
}else{
moveDish(level - 1, from, to, inter);
System.out.println("从" + from + "移动盘" + level +"到" + to);
moveDish(level -1, inter, from, to);
}
}
/**
*
* @param args
*/
public static void main(String[] args) {
int nDisk = 5;
moveDish(nDisk, 'A', 'B', 'C');
}
}