Recursive understand - Tower of Hanoi problem

Tower of Hanoi problem

Tower of Hanoi problem solving can be thought clever use of recursive

The following excerpt know almost elaborate on my view very clear answer:

Use the program to solve this problem, we first define a moving function: move (mobile number, start column, transit column, the goal posts),
for example, move (2, A, B, C) represent the two dishes from the A-pillar ( start column) by the B-pillar (pillar transit) to the C-pillar (the target column).
About the beginning of the column, these three concepts transfer column, the column can be used to move the target during a state to understand,

See below a diagram should be able to understand:
There are three states of the column, the column beginning, middle column, live target start column means is located at the beginning of the state of all the dishes column, the column transfer means is to temporarily store the intermediate state n -1 (three is 3-1) plate column,
the target column refers to the plate to move to the final column. It should be noted that, beginning column, transit column, the target column is not static, but will vary depending on the different levels. (If there is temporarily unable to understand the words, the first read on, you will be able to go back to understand).

Then we discuss the situation points to move the dish:

I.

When only one plate (calling move (1, A, B, C)) only when a plate of time,

  • As long as the plate moved directly from the beginning of the column to the column can be a target, and no intermediate state (i.e. without transit via column), the process may move the moving operation represented by a word print ( 'A ---> C') ;

Scenario 2

When there are two plates (calling move (2, A, B, C)) in this case three steps:

  • step1. In addition to the maximum plate A plate is moved from BA ---> B (column Start ---> transit column) [equivalent to calling the move (1, A, C, B)]
  • step2. The maximum plate is moved from A CA ---> C (column Start ---> target column) [equivalent to calling the move (1, A, B, C)]
  • step3. In addition to the largest plate of dishes from B to move CB ---> C (transit column ---> Target column) [equivalent to calling the move (1, B, A, C)] I would like for more both cases we should not be any doubt, is certain.

Then we look at three cases

Three cases

When there are three plates (call to move (3, A, B, C)) the case where the same three steps:

  • step1. In addition to the maximum plate A plate is moved from B (note that at this time for this step is the start of column A, C for the transit pillar, B-pillar as a target, so as to complete the top two plates from a ---> B task) a ---> C (column start ---> transit column) [equivalent to calling the move (1, a, B, C)] a ---> B (start column ---> target column) [equivalent to calling the move (1, A, C, B)] C ---> B (column transit ---> target column) [equivalent to calling the move (1, C, A, B)]
  • step2. The maximum plate is moved from A C (at this time for this step is the start of column A, B for the transit pillar, C pillar for the target, in order from the largest plate A ---> C) A- -> C (column start ---> target column) [equivalent to calling the move (1, A, B, C), i.e., direct execution print ( 'A ---> C')]
  • step3. In addition to the maximum tray plate moved from B to C (note that at this time for this step is the start of the column B, A for the transit pillar, C pillar for the target, so as to complete the transfer of the column in step2 the two plates from B ---> task C) B ---> a (column start ---> transit column) [equivalent to calling the move (1, B, C, a) B] ---> C (column start ---> target column) [equivalent to calling the move (1, B, A, C)] A ---> C (column transit ---> target column) [equivalent to calling the move (1, A, B, C)]

Three describe the situation may suddenly not so easy to understand, but we should be able to find the form of case and three step1 step3 and two forms of the whole entire situation like now? And to note the level of analysis is not the same, beginning column, transit column, the column is not the same goal.

For step1 column for transit is C, a column for transit step3 is A, the entire case of a three columns are for transit B.

We have previously determined the case where two functions are invoked move (2, A, B, C), which is equivalent to A ---> B A ---> CB ---> C three steps, three contingencies of step1 is a ---> C a ---> B C ---> B three steps, with the case of two forms of the same, according to the foregoing conversion situation II, and that this function can be transformed into three steps move (2, a, C, B) the case of three step3 Similarly, it would make the conversion function move (2, B, a, C)
and where three step2 with a direct print ( 'a ---> C ') instead move (1, A, B, C) so that the entire three cases can be represented so:

  1. move(2,A,C,B) //step1.
  2. In addition to the maximum plate is moved from A plate Bprint ( 'A ---> C') // step2.
  3. A plate from the maximum move Cmove (2, B, A, C) // step3.
  4. In addition to the largest plate of the plate is moved from B and C we know the situation is a function of three calls move (3, A, B, C),

Therefore, the above three lines is equivalent to the function actually move (3, A, B, C).

Here should be able to find a clue, the case of a four (4 layers) function call is move (4, A, B, C),

  1. Which indicates that move (3, A, C, B) // step1 pseudo-code.
  2. In addition to the maximum plate is moved from A plate Bprint ( 'A ---> C') // step2.
  3. A plate from the maximum move Cmove (3, B, A, C) // step3.
  4. In addition to the maximum plate is moved from B to C plate

There is little doubt this partnership can write your own situation every step concrete step four and then do the conversion, limited space is no longer listed here.

in conclusion

In fact, it can be concluded that: for n (n> 1) layer HANOR calling the function move (n, A, B, C) recursively solve this problem, this function is performed

  1. move(n-1,A,C,B) //step1.

  2. In addition to the maximum plate is moved from A plate Bprint ( 'A ---> C') // step2.

  3. A plate from the maximum move Cmove (n-1, B, A, C) // step3.

  4. In addition to the maximum plate is moved from B to C plate

Code

With the above understanding, after then, we can try to write code to solve the problem of, Python implementation:

def move(n,A,B,C):
    if n==1:             # 1个盘子,直接打印出移动动作
        print(A,'--->',C)
    else:                # n > 1时,用抽象出的3步来移动
        move(n-1,A,C,B) #step1.  把除了最大的盘子之外的盘子从A移到B
        print(A,'--->',C)  #step2.  把最大的盘子从A移到C
        move(n-1,B,A,C)#step3.  把除了最大的盘子之外的盘子从B移到C

so, read here, I venture to guess that most people should understand that the heart of the 7788

Here are implementation of the Java code

public class Hanoi {
    
    public static int move(int n,char A,char B,char C) {
        // 如果是移动过一个盘子
        if(n == 1) {
            System.out.println(A+"-->"+C);
            return 1;
        }else {
            // 一个以上的盘子
            int r1 = move(n-1,A,C,B);
            System.out.println(A+"-->"+C);
            int r2 = move(n-1,B,A,C);
            return r1+r2+1;
        }
    }
    
    
    public static void main(String[] args) {
        int count = 6;
        int result = Hanoi.move(count, 'A', 'B','C');
        System.out.println("total numbers of moves:"+result);
    }
}

Guess you like

Origin www.cnblogs.com/watertreestar/p/11780240.html