數據結構之漢諾塔(Java基礎)

public class HanoiTower {
	
	/**
	 * 移動盤子
	 * topN:移動的盤子數
	 * from:起始塔座
	 * inter:中間塔座
	 * to:目標塔座
	 */
	public static void doTower(int topN,char from,char inter,char to) {
		if(topN == 1) {
			System.out.println("盤子1,從"+ from + "塔座到" + to + "塔座");
		} else {
			doTower(topN - 1, from, to, inter);
			System.out.println("盤子" + topN +",從" + from + "塔座到" + to + "塔座" );
			doTower(topN - 1, inter, from, to);
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章