您的位置:首页 > 其它

poj1958Strange Towers of Hanoi

2014-11-16 22:13 337 查看
/**********************
 * Author:fisty
 * Data:2014-11-16
 * poj1958
 * 动态规划
 * *******************/
#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int table[15] = {0, 1, 3, 5};

int hanoi(int j, int i){
        int cost = 0;
        cost += table[i]*2;
        cost += pow(2, j)-1;

        return cost;
}
int main(){
        int cost;        
        for(int i = 1;i <= 12; i++){
                if(i > 3){
                        table[i] = INF;
                        for(int j = 1; j < i; j++){
                                //枚举先把j个移到一个柱子上再把剩下的移到最终柱子
                                cost = hanoi(j, i-j);
                                table[i] = min(cost, table[i]);
                        }
                }
                printf("%d\n", table[i]);
        }
        return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: