http://acm.hdu.edu.cn/showproblem.php?pid=2064
現在我們改變遊戲的玩法,不允許直接從最左(右)邊移到最右(左)邊(每次移動一定是移到中間杆或從中間移出),也不允許大盤放到下盤的上面。
Daisy已經做過原來的漢諾塔問題和漢諾塔II,但碰到這個問題時,她想了很久都不能解決,現在請你幫助她。現在有N個圓盤,她至少多少次移動才能把這些圓盤從最左邊移到最右邊?
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long int ll;
ll dp[36][2];
void init()
{
dp[1][0]=1,dp[1][1]=1;
for(int i=2;i<36;i++) dp[i][0]+=dp[i-1][0]*2+dp[i-1][1]+1,dp[i][1]+=dp[i-1][0]+2*dp[i-1][1]+1;
}
int main()
{
// freopen("1.txt","r",stdin);
init();
int n;
while(scanf("%d",&n)!=EOF)
{
cout<<dp[n-1][0]*3+3*dp[n-1][1]+2<<'\n';
}
return 0;
}