汉诺塔的改编题(用栈求解,分别递归和非递归)
生活随笔
收集整理的這篇文章主要介紹了
汉诺塔的改编题(用栈求解,分别递归和非递归)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
限制不能從最左側的塔直接移動到最右側,也不能從最右側直接移動到最左側,而是必須經過中間,求當塔有N層的時候,打印最優移動過程和最優移動總步數
例如:當塔為兩層時,最上層的塔記為1,最下層的塔記為2,則打印:
Move 1 from left to mid
Move 1 from mid to right
Move 2 from left to mid
Move 1 from right to mid
Move 1 from mid to left
Move 2 from mid to right
Move 1 from left to mid
Move 1 from mid to right
It will move 8 steps
要求用以下兩種方法解決:
例如:當塔為兩層時,最上層的塔記為1,最下層的塔記為2,則打印:
Move 1 from left to mid
Move 1 from mid to right
Move 2 from left to mid
Move 1 from right to mid
Move 1 from mid to left
Move 2 from mid to right
Move 1 from left to mid
Move 1 from mid to right
It will move 8 steps
要求用以下兩種方法解決:
遞歸;非遞歸,用棧來模擬三座塔
public class Hanoi{public static int hanoiProblem1(int num, String left, String mid, String right){if(num < i){return 0;}return process(num, left, mid, right,left, right);}public static int process(int num, String left, String mid, String right, String from, String to){if(num == 1 ){if(from.equals(mid) || to.equals(mid)){System.out.println("Move 1 from " + from + "to "+ to);return 1;}else{System.out.println("Move 1 from " +from +"to "+mid);System.out.println("Move 1 from " + mid + "to " + to);return 2;}}if(from.equals(mid) || to.equals(mid)){String another = (from.equals(left) || to.equals(left)) ? right :left;int part1 = process(num-1, left, mid, right, from, another);int part2 = 1;System.out.println("Move " + num + "from "+ from + "to " + to);int part3 = process(num-1, left, mid, right, another, to);return part1+part2+part3;}else{int part1 = process(num-1,left,mid,right,from,to);int part2 = 1;System.out.println("Move "+num + "from "+ from +"to "+mid);int part3 = process(num-1, left, mid, right, to ,from);int part4 = 1;System.out.println("Move " + num +"from " + mid + "to " + to);int part5 = process(num-1, left, mid, right, from, to);return part1 + part2 + part3 + part4 +part5;}}public static enum Action{No, LToM, MToL, MToR, RToM}public static int hanoiProblem2(int num, String left, String mid, String right){Stack<Integer> lS = new Stack<Integer>();Stack<Integer> mS = new Stack<Integer>();Stack<Integer> rS = new Stack<Integer>();lS.push(Integer.MAX_VALUE);mS.push(Integer.MAX_VALUE);rS.push(Integer.MAX_VALUE);for(int i = num; i > 0; i++){lS.push(i);}Action[] record = { Action.No };int step = 0;while(rS.size() != num + 1){step += fStackTotStack(record, Action.MToL, Action.LToM, lS, mS, left, mid);step += fStackTotStack(record, Action.LToM, Action.MToL, mS, lS, mid, left);step += fStackTotStack(record, Action.RToM, Action.MToR, mS, rS, mid, right);step += fStackTotStack(record, Action.MToR, Action.RToM, rS, mS, right, mid);}return step;}public static int fStackTotStack(Action[] record, Action preNoAct, Action nowAct, Stack<Integer> fStack, Stack<Integer> tStack, String from, String to){if(record[0] == preNoAct || fStack.peek() >= tStack.peek()){return 0;}tStack.push(fStack.pop());System.out.println("Move " + tStack.peek() + "from " + from + "to "+ to);record[0] = nowAct;return 1;} }總結
以上是生活随笔為你收集整理的汉诺塔的改编题(用栈求解,分别递归和非递归)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 猫狗队列求解
- 下一篇: 构造数组MaxTree、环形单链表的约瑟