I want to write a program that solves the puzzle of the hanoi towers without using recursion. I think I understand the recursive
version but I don't know how to convert it into a non recursive one.
PROGRAM Hanoi(input, output);
VAR N:integer;
PROCEDURE dohanoi(N, Tfrom, Tto, Tusing : integer);
BEGIN
if N > 0 THEN
BEGIN dohanoi(N-1, Tfrom, Tusing, Tto);
writeln('move ', Tfrom:1, ' --> ', Tto:1);
dohanoi(N-1, Tusing, Tto, Tfrom);
END END;
BEGIN write('N = ? '); r
eadln(N);
writeln;
dohanoi(N, 1, 3, 2)
END.
So the formula for finding the number of steps it takes to transfer N disks from post A to post B is: 2^n - 1.
I'll be glad if you can provide me with some links where I can find the program I'm looking for or paste the code itself. Or perhaps
give me some instructions because I don't have any ideas for solving the problem. Thank you in advance.
Start Free Trial