Summary of C + + Hanoi Tower problem knowledge points


The tower of Hanoi problem is one of the common tasks in psychological experimental research. Of course, we study computers, so we try to solve it with computers.


Openjudge6261 Hanoi Tower problem


There is an intelligence toy. There are three rods on a copper plate. On the leftmost rod, a tower composed of N discs is strung from top to bottom and from small to large. The purpose is to move all the discs on the leftmost rod to the middle rod, provided that only one disc can be moved at a time, and the large disc is not allowed to be placed on the small disc. This is the famous Hanoi Tower problem.

Suppose the disks are numbered 1, 2, 3


The input is an integer followed by three single character strings.

The integer is the number of plates, and the last three characters represent the number of three poles.


Output the record of moving the plate at each step. Move one line at a time.

The record of each movement is, for example, in the form of a – > 3 – > b, that is, move the plate numbered 3 from rod a to rod B.

sample input

2 a b c

sample output


Hanoi Tower problem

The solution algorithm of Hanoi Tower problem is a classical divide and conquer algorithm, and the divide and conquer algorithm has three most important steps:

  1. Decomposition: if we want to move num plates from the original column l to the target column R through the transition column mid, we can first move the upper (num – 1) plates from the original column l to the transition column mid, then move the plate numbered num to the target column R, and finally move the (num – 1) plates from the transition column mid to the target column R.
  2. Solution: use recursion to solve the subproblem and output. (boundary condition: when there is only one plate, i.e. num = = 1, it is good to output directly).
  3. Merge: the result of recursion is the result. There is no need to merge.

In short, every time we regard the num plate as a whole, the remaining (num – 1) plate as a whole, and then move the two whole separately to make it reach the target position.

Finally, calculate the time complexity. It’s a little difficult here.

Suppose that it takes step (I) for I plates to move from one column to another

For a single tower, the program does the following:

  1. Move the above (n – 1) plates to the transition column for step (n – 1).
  2. Move the nth plate to the target column for 1 times.
  3. Move (n – 1) plates on the transition column to the target column for step (n – 1).

Then the recursive formula can be obtained

step(n) = 2 * step(n – 1) + 1

Then keep pushing it down, and you’ll get

step(n) = 2^n * step(0) + 2^(n – 1) + 2^(n – 2) + …… + 2^1 + 2^0

And because 0 plates don’t have to be moved at all, so step (0) = 0

So step (n) = 2 ^ (n – 1) + 2 ^ (n – 2) +… + 2 ^ 1 + 2 ^ 0

After that, the formula of equal ratio sequence can be deduced: step (n) = 2 ^ n ^ – 1

We find that the number of moves is 2 ^ n ^ – 1, which is actually the least number of moves for the tower of Hanoi problem. Therefore, the time complexity of the algorithm to solve the Hanoi Tower problem is O (2 ^ n ^).


# include <cstdio>
# include <iostream> 
# include <cmath>
# include <cstring>
# include <algorithm>

using namespace std;

int n;
char a, b, c;

//Hanoi (Num, l, mid, R) indicates that num plates need to be moved from column L through column mid to column R.
void hanoi(int num, char l, char mid, char r)
  if (num == 1) printf("%c->%d->%c\n", l, num, r);
  else {
    hanoi(num - 1, l, r, mid);
    printf("%c->%d->%c\n", l, num, r);
    hanoi(num - 1, mid, l, r);

int main()
  scanf("%d", &n);
  cin >> a >> b >> c;
  hanoi(n, a, c, b); //  This is because the title is to move all the plates from the left column to the middle column, from a to B.
  return 0;

This is all the relevant knowledge points compiled by Xiaobian. Thank you for your learning and support for developeppaer.