HDU 2064 questions about the Nottingham Tower

Source: Internet
Author: User

1th Step: Initial state

2nd step: Move the upper n-1 to the 3rd pole.

3rd Step: Move the nth disk from 1 to 2

4th step: Move the front n-1 from 3 to 1, give way to the first disk.

5th step: Move the nth disk from 2 to 3

6th step: Move the front n-1 from 3 to complete the move

We set F (n) to the number of steps required to move n disks from left (1) to 3, and of course the number of steps to move from 3 to 1.
If you want to move the nth disk from left (1) to 3, you need to move the front n-1 from left (1) to the right (3), then from right (3) to left (1), and then from left (1) to right (3).
The nth disk goes from left (1) to Medium (2) and then right (3) through 2 steps.

So, f (n) =3*f (n-1) +1; F (n) =3^n-1 can be obtained by mathematical calculation;

#include
int main ()
{
 int i,n;
 __int64 f[36];
 f[1]=2;
 for (i=2;i<36;i++)
  f[i]=3*f[i-1]+2;
 while (scanf ("%d", &n)!=eof)
 {
  printf ("%i64d\n", F[n]);//f (N) =3^n-1;  
 }
 return 0;
}


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.