Traditional Culture Encyclopedia - Photography and portraiture - What is the formula for the Hanoi Tower problem?
What is the formula for the Hanoi Tower problem?
There are three poles a, b and C. 1) perforated disk, the size of which decreases from bottom to top. All discs need to be moved to C-bar according to the following rules:
1. Only one disc can be moved at a time;
2. Big plates can't be stacked on small plates.
Tip: The CD can be temporarily placed on the B pole, or the CD removed from the A pole can be moved back to the A pole, but the above two rules must be observed.
Q: How to move? How many times do I have to move?
Generally N=64. This requires at least 264- 1 time. In other words, if a disk can move in one second, it will still take 584.554 billion years. According to the Big Bang theory, the age of the universe is only 65.438+03.7 billion years.
In real toys, generally n = 8;; This will take 255 steps. If N= 10, you need to move 1023 times. If N= 15, it needs to be moved 32767 times; That is to say, if a person moves a disc every day from the age of 3 to 99, he can only move 15. If N=20, you need to move 1048575 times, which is more than one million times.
Let's look at Hanoi (1, one, two, three) first. At this time, a board on one column is directly moved to three columns. Note that it doesn't matter whether one or three columns here are A, B or C. Remember that the disk on the column represented by the second parameter of the function is moved to the column represented by the fourth parameter. For your convenience, record this operation as:
One = three
Look at the situation in Hanoi (two, one, two, three). Considering that the situation in Hanoi (1) has been analyzed, it is known that there will actually be three actions at this time, namely:
One = two
One = three
Two = three
Obviously, this is actually equivalent to moving two disks in one column directly into three columns.
Look at the situation in Hanoi (three, one, two, three). analyse
Hanoi (two, one, three, two)
One = three
Hanoi (two, two, one, three)
That is, first move two disks from one column to two columns, then move one disk from one column to three columns, and finally move two disks from two columns to three columns. Isn't this equivalent to moving three disks in one column directly to three columns?
Using induction, we know that for any n,
Hanoi (n- 1, one, three, two)
One = three
Hanoi (n- 1, two, one, three)
That is to say, first, move the n- 1 disk from one column to two columns, then move a disk from one column to three columns, and finally move the n- 1 disk from two columns to three columns. This is the result we need!
Respondent: Wu Chenghua 12 1- manager level 412-511:51.
Hanoi
The problem of Hanoi Tower is an ancient legend in India. Brahma, the god of creation, left three diamond rods in a temple. The first one is covered with 64 round pieces of gold, the largest one is at the bottom, and the other one is smaller than the other one. The monks in the temple took pains to move them from one stick to another, stipulating that the middle stick could be used as a help, but only one stick could be moved at a time, and the big one could not be placed on the small one. Please calculate the solution by yourself. See the end of the program. Faced with a huge number (the number of times to move the disc)1844674073709551615, it seems impossible for monks to complete the movement of the gold piece all their lives.
Later, this legend evolved into the game of Hanoi Tower:
1. There are three poles A, B, C, B, C, and there are some dishes on the pole A.
2. Move one plate at a time, and the small one can only be stacked on the big one.
3. move all plates from pole a to pole C.
Through research, it is found that the cracking of the Tower of Hanoi is very simple, that is, according to the moving rules, the gold nuggets are moved in one direction:
For example, the movement of the third tower in Hanoi: A → C, A → B, C → B, A → C, B→A, B → A, B → C, A → C.
In addition, the Tower of Hanoi problem is also a classic recursive problem in programming.
Supplement: Algorithm Implementation of the Tower of Hanoi (c++)
# include & ltfstream & gt
# include & ltiostream & gt
Use namespace std
of stream fout(" out . txt ");
void Move(int n,char x,char y)
{
Fout & lt& lt "Ba" <<n<& lt "number comes from" <<x<& lt "moved to"
}
void Hannoi(int n,char a,char b,char c)
{
If (n== 1)
Move( 1,a,c);
other
{
Hannoy (n- 1, a, c, b);
Move(n,a,c);
Hannoy (n- 1, b, a, c);
}
}
int main()
{
Fout & lt& lt "The following is the solution of the seven-story tower in Hanoi:"
Hannoi(7,' a ',' b ',' c ');
fout . close();
Cout & lt& lt "Output finished!" & lt& ltendl
Returns 0;
}
C language simplification algorithm
/* SS7E Copyright Owner */
# include & ltstdio.h & gt/* SS7E copyright owner */
Void hanoi(int n, char A, char B, char C) /* Copyright by SS7E */
{
If (n== 1)
{
Printf ("Move disk %d from %c to %c\n", n, a, c);
}
other
{
Hanoi (n- 1, a, c, b); /* SS7E Copyright Owner */
Printf ("Move disk %d from %c to %c\n", n, a, c);
Hanoi (n- 1, b, a, c); /* SS7E Copyright Owner */
}
}
Main()/* SS7E Copyright Owner */
{
int n;
Printf ("Please enter the number n to solve the N-order Hanoi Tower problem: \ n");
scanf("%d ",& ampn);
hanoi(n,' A ',' B ',' C ');
}/* SS7E Copyright Owner */
Defendant: Conqueror _-Juren Level 5 12-5 13:57.
Package::::::::
Hanoi plan;
function Hanoi(x:integer):longint;
begin
If x= 1, then Hanoi: =1;
If x=2, then Hanoi:= 3;;
other
begin
Hanoi: = 2 * Hanoi (x-1)+1;
End;
End;
begin
Read (x){ What number}
Write (Hanoi (x)););
End.
The idea is that the nth time is equal to the nth time-1time 2+ 1 time.
- Related articles
- Why is racial discrimination and gender discrimination so serious in America?
- Introduction to qimen tourist attractions.
- Why doesn't Apple 12 have a photography style option?
- What do APO and Ed mean in astronomical telescope? Why do some astronomical telescopes only have APO, some only have ED, and some have both APO and ED?
- Dreaming of the omen that the mobile phone will make electricity
- Shanghai 10,000-person factory resumed work in an orderly manner. How to do a good job in epidemic prevention at this stage?
- Who has the complete picture of Qifan? give a free
- University dormitory, what are you talking about before going to bed?
- How to write the artistic practice of comprehensive quality evaluation?
- Canon photography introductory course SLR camera shooting skills