POJ1958 Strange Tower of Hanoi(4塔汉诺塔)

原题链接:http://poj.org/problem?id=1958

解题思路 递推
在n个盘子3座塔的问题中,设d[n]表示n个盘子3塔Hanoi的最少步数,显然有d[n] = 2*d[n-1]+1, d[1] = 1.即把n-1个盘子从A移动B,再把最下面的盘子移到C,最后把n-1个盘子从B移到C。

本题中,设f[n]边数求解n盘4塔问题的最少步数,则:

f[n] = min1<=i<n {2*f[i] + d[n-i]}

#include <bits/stdc++.h>

using namespace std;

int d[13], f[13];

int main()
{
    memset(f, 0x3f, sizeof f);
    d[1] = 1;   
    f[1] = 1;
    for (int i = 2; i <= 12; i ++ )
    {
        d[i] = 2*d[i-1]+1;
    }
    cout << 1 << endl;
    for (int n = 2; n <= 12; n ++ )
    {
        for (int i = 1; i <= n; i ++ )
        {
            f[n] = min(2*f[n-i]+d[i],f[n]);
        }
        cout << f[n] << endl;
    }
    
    return 0;
}
上一篇:solaris – libstdc .so.6:open failed:没有这样的文件或目录


下一篇:「CEOI2010 day2」 Tower 题解