poj 1958 Strange Towers of Hanoi…

题意:四个柱子的汉诺塔 A B C D 开始A上有N个disk 全部移到D上最少的步数

思路: 首先你得明白三个柱子的汉诺塔 n个disk从A 到 C的最少步数  F(n) = 2^n -1;
那四个呢?? 其实题目已经告诉我们解法了 At first k >= 1 disks on tower A are fixed and the remaining n-k disks are moved from tower A to tower B using the algorithm for four towers.Then the remaining k disks from tower A are moved to tower D using the algorithm for three towers. At last the n - k disks from tower B are moved to tower D again using the algorithm for four towers
就是先移 n-k个到B(利用四个柱子) 然后移k到D (这时只能用三个柱子 因为B不能用) 然后再把B上的n-k个移到D

count[n] = min (count[n],2*count[n-k] + 2^k - 1) (0 < k <n)

//196K      0MS
#include <stdio.h>
#include <math.h>
const int inf = 1000;
int Min (int a,int b)
{
      return a > b ? b : a;
}
int main ()
{
      int n,k,count[15];
      count[1] = 1;
      for (n = 2;n <= 12;n ++)
      {
              count[n] = inf;
              for (k = 1;k < n;k ++)
              {
                      int min = 2*count[n-k] + pow(2,(double)k) - 1;
                      count[n] = Min (count[n],min);
              }
      }
      for (int i = 1;i <= 12;i ++)
              printf ("%d\n",count[i]);
      return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值