题目链接:https://www.acwing.com/problem/content/98/
设(d[n])表示(n)盘(3)塔问题的最小步数,(f[n])表示(n)盘(4)塔问题的最小步数
则(f[n] = min_{1 leq i< n}{2*f[i]+d[n-i]})
其中(f[1] = 1)
含义是先将(i)个盘子在四塔模式下移到(B)柱,再将(n-i)个盘子在三塔模式下移到(D)柱
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 20;
const int inf = 100000007;
int n;
int d[maxn],f[maxn];
int sta[maxn],tail = 0;
ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
int main(){
n = 12;
d[0] = 0;
d[1] = 1;
for(int i=1;i<=n;++i) d[i] = d[i-1] * 2 + 1;
f[1] = 1;
for(int i=2;i<=n;++i){
f[i] = inf;
for(int j=1;j<i;++j){
f[i] = min(f[i],2 * f[j] + d[i-j]);
}
}
for(int i=1;i<=n;++i) printf("%d
",f[i]);
return 0;
}