Topic 1458: Hanoi III

Source: Internet
Author: User

Title Description:

At the end of the 19 century, an intellectual toy was sold in European stores, with three poles on a piece of copper, and a tower of 64 discs strung from top to bottom on the leftmost pole. The aim is to move all the discs on the left pole to the right pole, provided that only one disk can be moved at a time, and that the market is not allowed to be placed on top of the small plate. Now we change the gameplay, not allow to move directly from the leftmost (right) side to the right (left) side (each move must be moved to the middle pole or move out from between), nor allow the market to be placed on the top of the lower plate. Daisy has done the original questions and Hanoi II, but encountered this problem, she thought for a long time can not solve, please help her now. Now that there are n disks, how many times does she move at least to move the discs from the leftmost to the far right?

Input:

Contains multiple sets of data, each time an N value (1<=N=35) is entered.

Output:

For each set of data, the output moves the least number of times.

Sample input:
1312
Sample output:
226531440
 
 
At the beginning of the question, the problem is very complicated, and even intend to use BFS, and then feel wrong, reference to other people's ideas to suddenly enlightened, OK, I am good food ...
The code is simple, just want to understand Hanoi mobile process will be solved!
Ideas are as follows:
If moving the K-disk from the first pillar to the third pillar requires fun (k) movement, then the first move of the K-1 Disc Road, the third pillar needs Fun (k-1) movement, and then the largest disk moved to the middle pillar need 1 times to move, and then move the k-1 disk back to the first pillar also need fun (k-1) Move, move the largest plate to the third pillar need 1 times to move, and finally move the k-1 disk to the third pillar need fun (k-1) move, so the recursive formula is fun (k) =3*fun (k-1) +2. While the exit of the recursion is K=1, F (1) =2
The code is as follows:
#include <stdio.h>long long Fun (int n) {//recursive solution count!!!    if (n = = 0) return 0;    if (n = = 1) return 2; Return 3*fun (n-1) + 2;}    int main () {int n;    A long-long count;        while (scanf ("%d", &n)! = EOF) {count = Fun (n);    printf ("%lld\n", count); } return 0;}

Topic 1458: Hanoi III

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.