Title Description
Description
Hanoi Tower Problem (also known as Hanoi problem), is a well-known problem. On the a,b,c three pillars, there are n different sizes of discs (assuming the radii are 1-n), and at first they are stacked on my A (), and your goal is to move all the plates from tower A to tower C within the minimum number of legally moving steps.
The rules for each step in the game are as follows:
1. Each step is allowed to move only one plate (from the top of one pillar to the top of another)
2. In the process of moving, you must ensure that the large plate cannot be on the top of the small plate (small can be placed on the large top, the maximum plate can not have any other size plate)
For the case of n=3, a valid moving sequence:
1 from A to C
2 from A to B
1 from C to B
3 from A to C
1 from B to A
2 from B to C
1 from A to C
Give a number n to find a moving sequence with a minimum number of steps
Enter a description
Input Description
An integer n
Output description
Output Description
The first line is an integer k, which represents the minimum number of moving steps.
The next K-line, one sentence per line, n from x to Y, indicates that the N-number plate is moved from the X-column to the Y-column. X, Y belongs to {a,b,c}
Sample input
Sample Input
3
Sample output
Sample Output
7
1 from A to C
2 from A to B
1 from C to B
3 from A to C
1 from B to A
2 from B to C
1 from A to C
Data range and Tips
Data Size & Hint
n<=10
Title Description
Description
Hanoi Tower Problem (also known as Hanoi problem), is a well-known problem. On the a,b,c three pillars, there are n different sizes of discs (assuming the radii are 1-n), and at first they are stacked on my A (), and your goal is to move all the plates from tower A to tower C within the minimum number of legally moving steps.
The rules for each step in the game are as follows:
1. Each step is allowed to move only one plate (from the top of one pillar to the top of another)
2. In the process of moving, you must ensure that the large plate cannot be on the top of the small plate (small can be placed on the large top, the maximum plate can not have any other size plate)
For the case of n=3, a valid moving sequence:
1 from A to C
2 from A to B
1 from C to B
3 from A to C
1 from B to A
2 from B to C
1 from A to C
Give a number n to find a moving sequence with a minimum number of steps
Enter a description
Input Description
An integer n
Output description
Output Description
The first line is an integer k, which represents the minimum number of moving steps.
The next K-line, one sentence per line, n from x to Y, indicates that the N-number plate is moved from the X-column to the Y-column. X, Y belongs to {a,b,c}
Sample input
Sample Input
3
Sample output
Sample Output
7
1 from A to C
2 from A to B
1 from C to B
3 from A to C
1 from B to A
2 from B to C
1 from A to C
Data range and Tips
Data Size & Hint
n<=10
#include <cstdio>#include<iostream>using namespacestd;voidHanoiintKCharACharBCharc) { if(k==1) cout<<k<<" from"<<a<<" to"<<c<<Endl; Else{Hanoi (k-1, a,c,b); cout<<k<<" from"<<a<<" to"<<c<<Endl; Hanoi (K-1, B,a,c); }}intMain () {intn,ans[101]={0}; CIN>>N; for(intI=1; i<=n;i++) Ans[i]=2*ans[i-1]+1; cout<<ans[n]<<Endl; Hanoi (N,'A','B','C');}
3145 the game of the Nottingham Tower