在漢諾塔規則的基礎上,限制不能從最左的塔移動到最右的塔上,必須通過中間的塔,移動的跨度只能是一個塔。當塔有N層的時候,打印最優移動過程和最優移動步數。java
不管多少層,都看做有兩層,最大的一層(命名爲X)、(N-1)層合併起來的做爲一層(命名爲Y),目標是將X移動到最右側,而後再把Y移動到最右側。git
import java.util.Stack; /** * 每次移動只能移動一個柱子,不能跨柱子移動 * @author zhanyongzhi */ public class HannoiOneStep { public void startMove(int count){ move(count, "A", "B", "C"); } private void move(int item, String from, String buffer, String to){ // if(1 == item){ System.out.println(String.format("move %d from %s to %s", item, from, buffer)); System.out.println(String.format("move %d from %s to %s", item, buffer, to)); return; } //general situation move(item - 1, from, buffer, to); System.out.println(String.format("move %d from %s to %s", item, from, buffer)); move(item - 1, to, buffer, from); System.out.println(String.format("move %d from %s to %s", item, buffer, to)); move(item - 1, from, buffer, to); } }
使用棧而不使用遞歸的方式進行移動,使用3個棧模擬3個塔,每一步的移動,都按照真實狀況進行。
按照規則,可能的移動動做限定爲LM、ML、MR、RM四種步驟(L、M、R分佈表示左中右),經過引入逆反原則和小壓大原則,能夠得出每次移動,只有一種可行步驟。github
當執行了LM,若是此時下一步執行ML,叫作逆反操做,這樣會使得漢諾塔還原爲上一步的形狀,白走多一步,這樣明顯不是最優的方法,因此不可以執行逆反操做,叫逆反原則。code
當移動時,小的塊老是在大塊之上,叫小壓大原則。orm
當上一步爲:LM,下一步的狀況分析:blog
其餘分析相似,省略...遞歸
package com.github.zhanyongzhi.interview.algorithm.stacklist; import java.util.Stack; /** * 使用棧模擬漢諾塔移動,將towerA所有層移動到towerC * @author zhanyongzhi */ public class HannoiStack { private Stack<Integer> towerA = new Stack<>(); private Stack<Integer> towerB = new Stack<>(); private Stack<Integer> towerC = new Stack<>(); private MoveType preMoveType = MoveType.LM; enum MoveType{ LM("Move From Left to Middle"), MR("Move From Middle to Right"), RM("Move From Right to Middle"), ML("Move From Middle to Left"); private final String name; MoveType(String s) { name = s; } public boolean equalsName(String otherName) { return (otherName == null) ? false : name.equals(otherName); } public String toString() { return name; } } public void init(int size){ for(int i=size; 0 < i; i--){ towerA.push(i); } } public void startMove(){ int layerSize = towerA.size(); while(layerSize != towerC.size()){ moveStack(MoveType.LM, MoveType.ML, towerA, towerB); moveStack(MoveType.MR, MoveType.RM, towerB, towerC); moveStack(MoveType.RM, MoveType.MR, towerC, towerB); moveStack(MoveType.ML, MoveType.LM, towerB, towerA); } } private void moveStack(MoveType tryMove, MoveType preventMove, final Stack<Integer> towerFrom, final Stack<Integer> towerTo){ if(preMoveType == preventMove) return; if(towerFrom.empty()) return; Integer sElement = towerFrom.peek(); if(!towerTo.empty()){ Integer dElement = towerTo.peek(); if(sElement > dElement) return; } preMoveType = tryMove; System.out.println(tryMove); towerFrom.pop(); towerTo.push(sElement); } }
在github中查看get