/**
* @author by cly, Date on 2021/9/28.
*/
public class HanNuoTa {
public static void main(String[] args) {
move("A", "C", "B", 4);
}
/**
* starPlace 起始位置
* endPlace 目标位置
*
* n 移动个数
*/
static void move( String starPlace, String endPlace, String centerCol, int n){
if(n<=2)
{
System.out.println(starPlace+"->"+centerCol);
System.out.println(starPlace+"->"+endPlace);
System.out.println(centerCol+"->"+endPlace);
}
else
{
//把n-1个移动到中间位置
move(starPlace, centerCol, endPlace, n-1 );
System.out.println(starPlace+"->"+endPlace);
//把n-1个移动到目标位置
move(centerCol, endPlace, starPlace,n-1 );
}
}
}
java汉诺塔
最新推荐文章于 2024-07-19 17:37:28 发布