Hanoi Tower 2 (four bars)

Source: Internet
Author: User

Problem description The classic question of the Nottingham is often present as a recursive classic example. Some may not know the story of the Hanoi tower problem. Hanoi is a story from the Hindu legend, when God created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. God commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars. There is prophecy that the universe will be destroyed in a flash of time when this thing is done. Others believe that the Brahman is still moving the disc at a moment's stop. Well, of course the legend is not credible, and now Hanoi is more of a toy present.   Gardon received a Hanoi toy as a birthday present. Gardon is a person afraid of trouble (well, is lazy people), it is clear that the 64 discs one by one to move until all the plates to reach the third pillar is very difficult, so Gardon decided to do a little harm, he found an identical pillar, Pass this pillar to move all the plates to the third pillar more quickly. The following question is: How many times will he need to move to move them to the third pillar when Gardon uses n plates in a game? Obviously, when there is no fourth pillar, the solution to the problem is 2^n-1, but now with the help of this pillar, how much is it?

Input contains multiple sets of data, one row per data, and the number of plates N (1<=n<=64).

Output for each set of data, outputs a number to the minimum number of moves required to reach the target.

Sample INPUT1 3 12

Sample OUTPUT1 5 81

Authorgardon

Sourcegardon-dygg Contest 2
Dynamic planning, transfer is g[n]=min{g[n-k]+g[n-k]+f[k]} (1<=k<n) where F[n] is the Hanoi//f[n]=f[n-1]+f[n-1]+1 of three pillars;----->f[n]=2^ n-1; #include <stdio.h> #include <math.h>int main () {    int n,i,k;    Double f[66],g[66];    f[1]=2.0;    for (i=2;i<=64;i++) f[i]=f[i-1]*2.0;    for (i=1;i<=64;i++) f[i]-=1.0;    G[1]=1;    for (i=2;i<=64;i++)    {        g[i]=g[i-1]*2+f[1];        for (k=2;k<i;k++)        {            double tmp=g[i-k]*2+f[k];            if (g[i]>tmp) g[i]=tmp;        }    }    while (scanf ("%d", &n)!=eof)    {        printf ("%.0lf\n", G[n]);    }    return 0;}

  

Hanoi Tower 2 (four bars)

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.