Recursion - Tower of Hanoi problem

Legend Tower of Hanoi: Tower of Hanoi problem, is derived from an ancient Indian educational toys; when Brahma created the world to do three diamond pillars , from the bottom up according to a post on the pile with 64 gold the size of the order disc . Brahma Brahman command to the disk from the bottom in order of size again placed on the other pillars . And predetermined, the disc can not be enlarged in a small disk, the first disk can move between a three columns .

Mathematical abstraction: As shown below, from left to right there are A, B, C three columns, wherein the upper column A small laminated to have a large n discs, are now required to move the disk in the A column on the post C only during a principle: You can only move a platter dish and not in a small dish above, find the mobile number of steps and movement;

Recursion: recursive function is a function call itself; if a large and complex problem can be Cengceng into a similar problem with the original smaller scale of the problem, then we can use recursion to solve; general recursive need to have the boundary conditions , recursive forward end (subproblems) and recursive return sections (recursive outlet); when the boundary condition is not satisfied, the forward recursion; when the boundary condition is satisfied, the recursive return.

Recursive function design tips:

  • Recurrent themes;
  • Recursive function parameters;
  • Recursive function export;
  • Recursion sequence analysis: analysis of small problem big problem, reduce the use of each treatment thought to reduce the scale;

Recursive algorithm to solve the problem of species:

  • Data is defined according to recursively defined; (the Fibonacci function)
  • Solution to the problem is achieved in accordance with the recursive algorithm; (Towers of Hanoi)
  • In the form of data structure in accordance with the recursive definition; (binary tree, illustrating a problem, the linear form: DFS search, merge sort, quicksort, etc.)

Tower of Hanoi recursive analysis:

Suppose there are n a disc, the Towers of Hanoi, recursive angle analysis:

  • The plates of the n-1 A moves to B; (by means of an auxiliary column C)
  • The n-th plate, moves from A to C;
  • To the n-1 by the moving plate B to C; (by means of auxiliary column A) 

Game Method: Games in claim> C moves from A-; Manual operation: First, determine the number of layers n, if the number of layers is odd, the uppermost A-> C; if the number of layers is even, the uppermost A-> B;

Solution Program:

#include <the iostream>
 the using  namespace STD;
 int m = 0 ; // number of movements 

void Move ( int Disk, char  from , char to) { 
    COUT << " Mobile times: " << (m ++) << " the block: " << << Disk " as follows mobile: " <<   from << " -> " << to << endl;
} 

// recursive solution Tower of Hanoi 
void hanoi ( int Disks, char from , char to, char Assist) {
     IF (== Disks . 1 ) // recursive outlet; 
    { 
        Move (Disks, from , to); 
         return ; 
    } 
    // recursive sub-problem; 
    Hanoi (disks- . 1 , from , Assist, to); // n-th tray. 1, moving from A to B, by means of auxiliary column C; 
    move (Disks, from , to); // the n-th plate, moves from A to C; 
    Hanoi (disks- . 1 , assist, to, from ); // put a plate n-1, B to C by the movement, by means of auxiliary column A; 
} 

intmain () { 
    COUT << " Tower of Hanoi: " << endl;
     int n-= . 3 ; 
    COUT << " Tower of Hanoi number of disc: " << n-<< endl;
     // create three columns; 
    char A = ' A ' ;
     char B = ' B ' ;
     char C = ' C ' ;
     // recursive solution of Hanoi, outputting the moving step;
     // n-th column is moved to the disk from the C column A, column B by means of auxiliary ; 
    Hanoi (n-, A, C, B); // HANOR recursive solution;
    return 0;
}
Tower of Hanoi Templates
bash- 3.2 $ the Towers of Hanoi C ++ .cc; ./a. OUT 
Tower of Hanoi: 
HANOR number of disc: a 
mobile number: 1 to place the block: 1 as follows Mobile: A -> C 
the bash - 3.2 $ the Towers of Hanoi C ++ .cc; ./a. OUT 
Tower of Hanoi: 
HANOR disc number: 2 
the number of movements: one to place the block: 1 as follows mobile: A -> B 
mobile times: 2 the block: 2 according to the following movement: A -> C 
mobile times: . 3 the blocks: 1 as follows mobile: B -> C 
the bash - 3.2 $ the Towers of Hanoi C ++ .cc; ./a. OUT 
HANOR question: 
Tower of Hanoi disk number:3 
mobile number: 1 to place the block: 1 as follows Mobile: A -> C 
Mobile number: 2 to place the block: 2 according to the following movement: A -> B 
times of movement: 3 to place the block: 1 as follows Mobile: C - > B 
mobile times: . 4 to place the block: 3 as follows mobile: A -> C 
mobile times: . 5 to place the block: 1 as follows mobile: B -> A 
number of shifts: . 6 to place the block: 2 according to the following movement: B - -> C 
mobile times: . 7 to place the block: 1 in accordance with the following movement: A -> C
Tower of Hanoi Sample Output

It can be found by the results: The total number of moves is: 2 ^ n-1 times; n is the number of disks;

Program Address: https://github.com/yaowenxu/codes/blob/master/ recursive / Tower of Hanoi problem .cc 

Kept up to date, please indicate the source; for more information, please attention cnblogs.com/xuyaowen;

Reference links: * Figure from the text reference links, such as tort please private letter I replaced;

Tower of Hanoi illustration  of how to understand the Tower of Hanoi recursively?

Guess you like

Origin www.cnblogs.com/xuyaowen/p/hanoi-tower.html