题意:四个柱子的汉诺塔 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;
}
思路: 首先你得明白三个柱子的汉诺塔 n个disk从A 到 C的最少步数
那四个呢?? 其实题目已经告诉我们解法了 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
#include <stdio.h>
#include <math.h>
const int inf = 1000;
int Min (int a,int b)
{
}
int main ()
{
}