hdu 2064: Tower of Hanoi III

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

This feels to find the law of the question, too difficult, and sometimes may play table will be able to find the law, but this question: the idea did not hit the table, feeling a bit difficult, still not well understood meaning of the questions

Reference of this blog:
https://www.cnblogs.com/jackge/p/3218090.html

Title changed the original HANOR rules, but each must be in the middle of the column, although a slight change in the process is the same but the push (now has A, B, C three pillars, and reference numeral 1 -N plate), since the number N can not be moved to the plate C, is must be moved to the N B, so would N- 1 prior to this state on a plate C, and then moved to the N C and put on before the N-1 is moved to the plate a, to achieve the ultimate goal, then, the N-1 will then move to the C plate.

The above process to give a recursive formula F[N]= 3* F[N-1]+ 2

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
ll f[50];
int main(){
    f[1]=2;
    for(int i=2;i<=35;i++){
        f[i]=f[i-1]*3+2;
    }
    int n;
    while(~scanf("%d",&n)){
        printf("%lld\n",f[n]);
    }
    return 0;
}
发布了127 篇原创文章 · 获赞 32 · 访问量 1万+

Guess you like

Origin blog.csdn.net/boliu147258/article/details/102643463