汉诺塔的最简的步骤思路

public class Hanoi{
/**
* 参数说明:
* n:多个盘子
* from:原杆(其上有多个盘子的杆)
* denpend:中间杆
* to:目标杆
*/
public static void hanoi(int n,char from,char denpend,char to){
if(n==1){
System.out.println("将"+n+"号盘子"+from+"---->"+to);
}else{
hanoi(n-1,from,to,denpend);//将N-1盘子从原杆移动到中间杆
System.out.println("将"+n+"号盘子"+from+"---->"+to);//将第N号盘子从原杆移动至目标杆
hanoi(n-1,denpend,from,to);//将N-1盘子从原杆移动到中间杆
}
}

public static void main(String[] args){
char from='A';
char denpend='B';
char to='C';
System.out.println("盘子移动的情况如下:");
hanoi(4,from,denpend,to);//如果4个的情况下
}

}

原文地址:https://www.cnblogs.com/1147blog/p/6670100.html