Tower of Hanoi detailed analysis steps

 Three columns: A, B, C
 Plate number: n
 
 1. Through the whole idea, the n-1 look as a whole, apparent object
 	The move from A n-1 B
 	A move from the biggest C
	B n-1 from the moved C
 
 2. The more puzzling is this:
        B n-1 how to move from A
        n-1 and how to move from B to C.
 
 3. n-1 explains how to move from A to B
 A n-2 from the moved C
 A move from the maximum B
 Moved from the n-2 C B
 
 4. explain how C n-1 from the mobile B
 Moved from the n-2 C A
 C is moved from the maximum B
 Moved from the n-2 A B
 
 Step 1 can be seen to move from A C
 Step 3 can be seen as moved from B A
 Step 4 can be seen to move from B C
 
 From these we can build a common function:
 move(int nNumb, string strFrom, string strTo);
 Parameters: nNumb mobile number
         strFrom where to move
         strTo moved to where to go
 

 
 The problem can be summarized as:
                                         { move(n-2, "A", "C")
                   { move(n-1, "A", "B") { move(1,   "A", "B")
                   {                     { move(n-2, "C", "B")
 move(n, "A", "C") { move(1,   "A", "C")                            ...
                   {                     { move(n-2, "B", "A")
                   { move(n-1, "B", "C") { move(1,   "B", "C")
                                         { move(n-2, "A", "C")
 
 
 As can be seen from the upper part of the structure and through which part of the structure, it can be seen that this is a recursive form:
 
                     { move(n-1, Begin, Other)
 move(n, Begin, End) { move(1,   Begin, End  )
                     { move(n-1, Other, End  )
 
 Then the termination condition in which it recursively? We deduction to follow this last step, the structure looks like this:
 
                     { move(1, Begin, Other)
 move(2, Begin, End) { move(1, Begin, End  )
                     { move(1, Other, End  )
 
 Obvious, move (1, ..., ...); no longer necessary to go on to perform. So it has become a termination condition is recursive.
Published 25 original articles · won praise 7 · views 2104

Guess you like

Origin blog.csdn.net/qq_41506111/article/details/104031041