ID 2064 Tower of Hanoi III

Problem Description

About 19 century, sold in stores in Europe an intellectual toy, there are three bars on a copper plate, the leftmost rod from top to bottom, in ascending order of the column by a string consisting of 64 discs. The aim is to stem the leftmost bar to the right to move all of the disk, provided that you can only move one disk, and do not allow small cap on top of the market.

Now we change the rule of the game is not allowed to the far right (left) side directly from the most left (right) side (each move must be moved or removed from the middle of the middle bar), are not allowed into the footwall of the market above.
Daisy has been done and the original Tower of Hanoi Tower of Hanoi II, but when confronted with the question, she thought for a long time can not be resolved, now you help her. Now there are N discs, at least how many times she can move these disks to move from the leftmost rightmost?

Input

Comprising a plurality of sets of data, each input of a value N (1 <= N = 35).

Output

For each test, the minimum number of movement of the output.

Sample Input

1 3 12

Sample Output

2 26 531440

Problem-solving ideas:

  The column was assumed that the n-layer B needs to be moved from A C F [n] step. As for the specific see this movement can be: the top layer is moved from the n-1 through A B C F required [n-1] Step, A and then moved from the n-th layer B, a step required, then the n- layer was moved from a B C A, need f [n-1] step B and then moved from the n-th layer C, required step, and then moved on from layer n-1 through A B C, we need f [n-1] step, a total of 3 * f [n-1] + 2-step, where f [1] = 2;

 1  unsigned long long num[36];
 2 int main()
 3 {              
 4     num[1]=2;
 5     num[2]=8;
 6     for(int i=3;i<=35;i++)
 7         num[i]=3*num[i-1]+2;
 8     int n;
 9     while(~scanf("%d",&n))
10         cout<<num[n]<<endl;
11     return 0;
12 }

 

 
 

Guess you like

Origin www.cnblogs.com/zhigengniao/p/11278431.html