汉诺塔非递归算法.我只是将盘子的数量等于2,3的情况代到网上别人给的算法中验证了一下,没有错。并没有证明算法的正确性。算法是否有效,有待大家证明。 FaQe_;
2~1SQ.Q<RY
include <iostream> ll<Xz((o
#include <stdlib.h> oim9<_
t?x<g <PJ4
#ifdef _WIN32 0KcyLAJ
using namespace std; ,c$_t+
#endif j_!F*yul
fF$<7O)+]
static void hanoi(int height) L_uVL#To
{ 5j<mbt}
int fromPole, toPole, Disk; :uq\+(9
int *BitStr = new int[height], //用来计算移动的盘的号码 ,]ma+(|
*Hold = new int[height]; //用来存贮当前的盘的位置。hold[0]为第一个盘所在的柱号 tqvN0vY5
char Place[] = {'A', 'C', 'B'}; D9CaFu
int i, j, temp; {W=%U|f
u~M
q*
for (i=0; i < height; i++) Pw7]r<Q
{ .9 on@S
BitStr = 0; J!v3i*j\
Hold = 1; iwZPpl";
} F3v!AvA|
temp = 3 - (height % 2); //第一个盘的柱号 x=hiQ>BIO0
int TotalMoves = (1 << height) - 1; -aPg#ub
for (i=1; i <= TotalMoves; i++) ?Wr+Q
{ b8`)y<