poj1958
题意:4柱子hanoi,给出n<=12,求最少步数。
分析:我们知道3柱子的hanoi,步数=2^n-1。题中给出四个的解题思路,用dp求解,题中说先把n分成两部分,A部分用4柱子方法挪到2柱子,B部分用3柱子法挪到4柱子,然后用4柱子法把A挪到4柱子。假设B部分有k个,则g[i] = g[i - j] * 2 + f[j];
g[i]表示4柱子法,f[i]是3柱子法,挪动i个盘子的步数。
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
usingnamespace std;
int f[15], g[15];
int main()
{
//freopen("t.txt", "r", stdin);
f[0] =0;
int ans;
for (int i =1; i <=12; i++)
f[i] = (1<< i) -1;
g[0] =0;
for (int i =1; i <=12; i++)
{
g[i] =0x3f3f3f3f;
for (int j =1; j <= i; j++)
g[i] = min(g[i], g[i - j] *2+ f[j]);
printf("%d\n", g[i]);
}
return0;
}