题意:现在汉诺塔变成4个柱子的,(原本3个柱子的最小步数为 2^n-1),该怎么办啦?原来题目中有解法,(At first k >= 1 disks on towerA are fixed and the remaining n-k disks are moved from tower A totower B using the algorithm for four towers.Then the remaining kdisks from tower A are moved to tower D using the algorithm forthree towers. At last the n - k disks from tower B are moved totower D again using the algorithm for four towers)意思是,先将k个移到b柱子上,然后b柱子不懂,将剩余的n-k个从A移到D,这就相当是3个柱子的。最后还需要将B柱子上的k个转移到D上面去。
思路:上面官方已经把思路给你了,如果还不会的话,请再次读题。
状态转移方程: dp[i] = min(dp[i],2*dp[k]+mypow(2,i-k)-1); (1<=k<n)
#include<cstdio>
#include<cstring>
#include<iostream>
#define INF 100000000000
#define min(a1,b1) (a1)>(b1)?(b1):(a1)
using namespace std;
int n;
long long dp[13];
long long mypow(long long a,int t)
{
if(t==1)
return a;
else if(t==0)
{
return 1;
}
else
return a*mypow(a,t-1);
}
int main(void)
{
n = 12;
dp[1] = 1;
for(int i=2;i<=n;++i)
{
dp[i] = INF;
for(int k=1;k<i;++k)
{
dp[i] = min(dp[i],2*dp[k]+mypow(2,i-k)-1);
}
}
for(int i=1;i<=12;++i)
cout<<dp[i]<<endl;
return 0;
}