ACM hdoj Tower II

Source: Internet
Author: User

 

Tower II

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1207

The typical tower issue of Problem description is often used as a recursive classic example. Some people may not know the story of the tower. A story from the legend of India tells the story that, when God created the world, he made three diamond pillars with 64 gold disks stacked from bottom to top in order of size. God ordered the Brahman to re-place the disc from below in order of size on another pillar. It is also stipulated that the disc cannot be enlarged on a small disc, and only one disc can be moved between the three pillars. It was predicted that the universe would flash into destruction at the completion of the event. Some people believe that the Brahman still keeps moving the disc for a moment. Well, of course, this legend is not credible, and now the tower is more of a toy. Gardon received a birthday present from the toy tower.
Gardon is a troublesome person (well, a lazy person). Obviously, it is difficult to move 64 discs one by one until all the plates reach the third pillar, so gardon decided to make a small disadvantage, and he found another identical pillar, through which he quickly moved all the dishes to the third pillar. The following question is: How many times does gardon need to move each of the N plates in a game to the third pillar? Obviously, when there is no fourth pillar, the solution to the problem is 2 ^ N-1, but now with the help of this pillar, what is it?

 

Input contains multiple groups of data, each of which The number of data rows is n (1 <=n <= 64 ).
Output For each group of data, an output number is required to reach the target. The minimum number of moves. Sample Input
 
1312
Sample output
 
1581

 

Idea: think of the plate on the first tower as two parts. The first part (the plate above) can be moved to the second tower through two empty towers, and the second part (the big plate below) move a tower to the fourth tower, and then move the first part of the tower to the fourth Tower through two empty towers. Because there are many situations that can be divided into two parts, therefore, compare each case to obtain the smallest moving number.

 

 

Code:

# Include <stdio. h> # include <math. h> int main () {int I = 0, j = 0, n = 0; unsigned _ int64 A [65] = {0}, min = 0; A [1] = 1, a [2] = 3; for (I = 3; I <= 64; I ++) {min = 2 * A [1] + (unsigned _ int64) Pow (2, (I-1)-1; for (j = 2; j <I; j ++) {If (2 * A [J] + (unsigned _ int64) Pow (2, (I-j)-1 <min) min = 2 * A [J] + (unsigned _ int64) Pow (2, (I-j)-1 ;}a [I] = min ;} while (scanf ("% d", & N )! = EOF) {printf ("% i64u \ n", a [n]);} 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.