河内塔 - 替代解决方案

时间:2015-06-08 06:52:40

标签: algorithm towers-of-hanoi

Label the pegs A, B, C
let n be the total number of discs
number the discs from 1 (smallest, topmost) to n (largest, bottommost)
To move n discs from peg A to peg C:

一般递归解决方案是:

move n−1 discs from A to B. This leaves disc n alone on peg A
move disc n from A to C
move n−1 discs from B to C so they sit on disc n

为什么我们不能以这种方式考虑解决方案?

Move the top disk from A to B
Move the other n-1 disks from A to C
Move the single disk from B to C

我知道第二种解决方案是错误的,但我无法弄清楚原因。有人可以指出为什么第二种解决方案不正确吗?

1 个答案:

答案 0 :(得分:4)

在你的第二个解决方案中,你将占用B钉,它不能用于n-1碟。

这是因为您将最小的磁盘保留在B peg中。因此,现在没有其他光盘可以移动到B,因为所有其他光盘都比它大。

相关问题