Recursive and non-recursive solving the Towers of Hanoi

Limits can not be moved from the left-most column of the rightmost directly, nor from the far right to the left to move directly, but must go through an intermediate. When seeking tower N layer when the printing movement and most optimum total number of steps moved.
Here Insert Picture Description
Method a:
Recursive
analysis: N layer if left column, from the uppermost to the lowest order of 1 ~ N, then:
1. If the remaining N layer in the column, "left", it is desirable to move in all, there are three steps :
(1) 1 ~ N-1 layer, all of the first column from left to right. (Recursively)
(2) the N-th layer is moved in the left column
(3) the 1 ~ N-1 layer is moved in the right column all (recursive)
Note: If the remaining N layer moves from the left column, which moved to the right, move to the right, the process is the same as above.
2. If the remaining N-tier tower in the "left" in the hope of all to right
(1) to 1 ~ N-1 layer from left to right all the first tower. (Recursively)
(2) the N-th layer is moved in the left column
(3) the 1 ~ N-1 layer from right to left column all. (Recursively)
(4) from the first column to right N layer
(5) 1 ~ N-1 layer, all of the left column to right. (Recursively)
code is as follows:

import java.util.Scanner;
public class hannuo {
    public int process(int n,String left,String mid,String right,String from,String to){
       if(n==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 "+from+ " to "+to);
             return 2;
           }
       }
        int result=0;
       if(from.equals(mid)||to.equals(mid)) {

           String temp = (from.equals(left) || to.equals(right)) ? right : left;
           int step1 = process(n - 1, left, mid, right, from, temp);    //递归
           int step2 = 1;
           System.out.println("Move" + n + "from" + from + " to " + to);
           int step3 = process(n - 1, left, mid, right, temp, to);
           result=step1+step2+step3;
           System.out.println("移动总步数"+ result);
       }
       else{
           int step1 = process(n - 1, left, mid, right, from, to);    //递归
           int step2 = 1;
           System.out.println("Move" + n + "from" + from + " to " + mid);
           int step3=process(n-1,left,mid,right,to,from);
           int step4=1;
           System.out.println("Move"+ n +" from "+ mid +" to "+to);
           int step5=process(n-1,left,mid,right,from,to);
           result=step1+step2+step3+step4+step5;
           System.out.println("移动总步数"+ result);


       }
     return result;
    }
    public int hannuo1(int n,String left,String mid,String right){
        if(n<1){
    return 0;
        }
        return process(n,left,mid,right,left,right);
    }

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("please input the nummber:");
        int n=scan.nextInt();
     hannuo han=new hannuo();
       han.hannuo1(n,"left","mid","right");
    }
}

The result:
Here Insert Picture Description
two: non-recursive
non-recursive we naturally think of the stack, the left, center, and right place to stack abstract
Here Insert Picture Description
code implementation:

class State
{
    public int n;
    public String left;
    public String mid;
    public String right;
    public State(int n, String left, String mid, String right)
    {
        this.n = n;
        this.left =left;
        this.mid =mid;
        this.right =right;
    }
}
//栈
class StateStack {
    private State[] stack = new State[1000];
    //栈顶
    private int top = 0;

    //入栈
    public void push(State s) {
        stack[top++] = s;
    }

    //出栈
    public State pop() {
        if (top > 0) {
            return stack[--top];
        }
        return null;
    }
}
public class hannuota2 {

    public static void hannuota2(int n, String left,String mid, String right) {
        //创建一个栈
        StateStack s = new StateStack();
        //将开始状态进栈
        s.push(new State(n,"left", "mid","right"));
        //保存出栈元素
        State state = null;
        //出栈
        while ((state = s.pop()) != null) {
            //如果n为1,直接移动left->right
            if (state.n == 1) {
                move(state.left, state.right);
            }
            //如果n大于1,则按照递归的思路,先处理hanoi(n-1,left,right,mid),再移动left->right(等价于hanoi(1,left,mid,right) ),然后处理hanoi(n-1,mid,left,right),因为是栈,所以要逆序添加
            else {
                //栈结构先进后出,所以需要逆序进栈
                s.push(new State(state.n - 1, state.mid, state.left, state.right));
                s.push(new State(1, state.left, state.mid, state.right));
                s.push(new State(state.n - 1, state.left, state.right, state.mid));
            }
        }
    }

    public static void move(String str1, String str2) {
        System.out.println(str1 + "->" + str2);
    }


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("please input the nummber:");
        int n = scan.nextInt();

        hannuota2(n, "left", "mid", "right");
    }
}


Guess you like

Origin blog.csdn.net/weixin_42373873/article/details/89923727