Use recursion in the C language to solve the tower leader problem.

Source: Internet
Author: User

Use recursion in the C language to solve the tower leader problem.
(1) Introduction to Linglong

Hanoi Tower is a legend in India:

In the holy temple at the center of the world, benalrus (in Northern India), three gem needles are inserted on a copper sheet. In the creation of the world, Fan Tian, the Hindu god, wore 64 gold tablets from the ground up to the ground on one of the needles. No matter day or night, there is always a monk moving the gold Tablets according to the following rules: one piece at a time, no matter which needle, the small pieces must be on the large film. The monks predicted that the world would be wiped out in a bang when all the gold pieces were moved from the needle worn by fan Tian to another, and the Vatican, temples, and sentient beings will all be lost together.

Consider moving 64 gold Slices from one needle to another, and keep the order of size up and down. How many moves are required? Here we need a recursive method. Assume there are n slices, and the number of moves is f (n ). obviously f (1) = 1, f (2) = 3, f (3) = 7, and f (k + 1) = 2 * f (k) + 1. It is not hard to prove that f (n) = 2 ^ n-1.

If n = 64, how long does it take to move every second?

2 ^ 64-1 = 18446744073709551615 seconds!

An average year has 365 seconds for 31536000 days, and a leap year has 366 seconds for 31622400 days, with an average of 31556952 seconds per year.

This indicates that it takes more than 584.554 billion years to move the gold. The earth is only 4.5 billion years old, and the life expectancy of the solar system is said to be tens of billions of years. After 584.554 billion years, not to mention the solar system and the Milky Way, at least all life on the Earth, along with the Vatican, temples, and so on, has been wiped out!

(2) algorithm ideas

Represent the disc number in the order from top to bottom as 1, 2, 3 ......, N-1, n.

Divide all the plates into two parts: the n-1 disc on the top and the n-disc on the bottom ).

(1) Move the n-1 Disc above from column A to Column B. This process requires the help of column C.

(2) Move the nth disc from column A to column C. In this way, the nth disc is placed on the target position.

(3) Move the n-1 Disc above from Column B to column C. This process requires the help of column.

Here (1) uses recursion, which can be split into multiple steps (1), (2), and (3). When n is 1, recursion ends.

Similarly, (3) uses recursion, which can be split into multiple steps (1), (2), and (3). When n is 1, recursion ends.

(3) c language implementation
# Include
 
  
Void hanoi (int n, char pillar1, char pillar2, char pillar3); // function declaration void move (int n, char pillar_from, char pillar_to); // function declaration int count; // global variable int main () {int n; // input the number of gold slices in the Tower printf ("Please input the layer number of Hanoi Tower :"); scanf ("% d", & n); // objective: to move n gold Slices from A to C hanoi (n, 'A', 'B ', 'C'); return 0;} void hanoi (int n, char pillar1, char pillar2, char pillar3) {if (n = 1) {move (n, pillar1, pillar3);} else {// with the help of pillar3, move the n-1 gold pieces above from pillar1 to pillar2 hanoi (n-1, pillar1, pillar3, pillar2 ); // move the nth gold tablet from pillar1 to pillar3 move (n, pillar1, pillar3); // With pillar1, move n-1 gold Slices from pillar2 to pillar3 hanoi (n-1, pillar2, pillar1, pillar3);} void move (int n, char pillar_from, char pillar_to) {count ++; // count the number of moves printf ("step % d: move layer % d, % c --> % c \ n", count, n, pillar_from, pillar_to );}
 

Running result:

Please input the layer number of Hanoi Tower: 1step 1: move layer 1, A-->CPlease input the layer number of Hanoi Tower: 2step 1: move layer 1, A-->Bstep 2: move layer 2, A-->Cstep 3: move layer 1, B-->CPlease input the layer number of Hanoi Tower: 3step 1: move layer 1, A-->Cstep 2: move layer 2, A-->Bstep 3: move layer 1, C-->Bstep 4: move layer 3, A-->Cstep 5: move layer 1, B-->Astep 6: move layer 2, B-->Cstep 7: move layer 1, A-->C

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.