Today it's snowing, but it's not long.
Today, the rockets first won and then lost, and Yao Ming was outstanding.
Two things I didn't want to happen yesterday have all happened!
When I was studying in the evening, I read a C-language book. I saw a problem with the tower. I didn't understand it. I came back and turned to the Internet. Now I have posted the solution.
Question: at the end of the 19th century, I sold an intellectual toy in a store in ozhou, with three poles on a copper plate, on the leftmost bar, the Tower consists of 64 disks in ascending order. The purpose is to move all the disks on the leftmost bar to the right bar, with the condition that only one disk can be moved at a time and the tray cannot be placed on a small disk.
* Problem analysis and Algorithm Design
This is a well-known issue, which occurs in almost all teaching materials. The condition is that only one disk can be moved at a time and the disk cannot be placed on a small disk. Therefore, the number of moves for 64 disks is:
18,446,744,073,709,551,615
This is an astronomical number. If every microsecond can be calculated (not output) and moved once, it will take almost 1 million years. We can only find a solution to the problem and solve the tower with a smaller N value, but it is difficult to use a computer to solve the 64-layer tower.
Analyze the problem and find out the correct Algorithm for moving the plate.
First, consider the plate below a pole rather than the top plate on the pole, so the task becomes:
* Move the 63 plates above to B;
* Move the remaining plate on pole a to pole C;
* Move all the plates on rod B to Rod C.
To continue this process, we need to move 63 dishes, 62 dishes, and 61 dishes first.
To better describe the algorithm, you can define a function movedisc (n, a, B, c ). This function is used to move n plates from a pole to B using a C rod. In this way, you can move n dishes in the following process:
1) movedisc (n-1, A, C, B );
2) Move a plate from A to B;
3) movedisc (n-1, C, B, );
Repeat the above process until all the dishes are moved in place.
* Program Program comments
# Include <stdio. h>
Void movedisc (unsigned N, char fromneedle, char toneedle, char usingneedle );
Int I = 0;
Void main ()
{
Unsigned N;
Printf ("Please enter the number of disc :");
Scanf ("% d", & N);/* enter N values */
Printf ("\ tneedle: \ ta \ t B \ t C \ n ");
Movedisc (n, 'A', 'C', 'B');/* Move n plates from A to C Using B */
Printf ("\ t Total: % d \ n", I );
}
Void movedisc (unsigned N, char fromneedle, char toneedle, char usingneedle)
{
If (n> 0)
{
Movedisc (n-1, fromneedle, usingneedle, toneedle );
/* Move N-1 plates from fromneedle to usingneedle with toneedle */
++ I;
Switch (fromneedle)/* move a plate on fromneedle to toneedle */
{
Case 'A': Switch (toneedle)
{
Case 'B': printf ("\ t [% d]: \ t % 2D ......> % 2D \ n ", I, n, n );
Break;
Case 'C': printf ("\ t [% d]: \ t % 2D ......> % 2D \ n ", I, n, n );
Break;
}
Break;
Case 'B': Switch (toneedle)
{
Case 'A': printf ("\ t [% d]: \ t % 2D <......> % 2D \ n ", I, n, n );
Break;
Case 'C': printf ("\ t [% d]: \ t % 2D ......> % 2D \ n ", I, n, n );
Break;
}
Break;
Case 'C': Switch (toneedle)
{
Case 'A': printf ("\ t [% d]: \ t % 2D <............ % 2D \ n ", I, n, n );
Break;
Case 'B': printf ("\ t [% d]: \ t % 2D <........ % 2D \ n ", I, n, n );
Break;
}
Break;
}
Movedisc (n-1, usingneedle, toneedle, fromneedle );
/* Move the N-1 plates from usingneedle to the toneedle with fromneedle */
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.