三个柱子移动盘子 只有在调用的时候才移动一次

思路分析:
大盘子是3;中间盘子是2; 小盘子是1
from A柱子 to C柱子,B是辅助柱
一个盘子的时候:
盘子直接 从A柱子到C柱子
两个盘子的时候:
第一步 盘子1 fromAtoB : n-1 from A to B
第二步 盘子2 fromAtoC :n from A to C 被打印出来 有完工的苗头了
最后一步 盘子1 from BtoC :n-1 from B to C
/公共类 public class Re{ //定义一个main方法 public static void main(String[] args){ int howPan=3; //将盘子从A柱子移动C柱子 借助B柱子 movePan(howPan,"A","C","B"); } static void movePan(int howPan,String from,String to,String jieZhu){ if(howPan==1){ System.out.println("移动盘子1从"+from+"到"+to); return; } //移动第n-1个盘子从from柱 放到辅助柱上 movePan(howPan-1,from,jieZhu,to); //移动第n个盘子从from 放到目标柱上 System.out.println("移动盘子"+(howPan)+"从"+from+"到"+to); //移动第n-1个盘子从辅助柱 到目标柱上 movePan(howPan-1,jieZhu,to,from); } }
浙公网安备 33010602011771号