Advertisement

05.28.2003 at 06:40AM PDT, ID: 20628825
[x]
Attachment Details

Towers of Hanoi non recursive solution

Asked by ognianbg in Pascal Programming Language

Tags: hanoi, recursive

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
 
Keywords: Towers of Hanoi non recursive solution
 
Loading Advertisement...
 
[+][-]05.28.2003 at 09:29PM PDT, ID: 8603304

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.29.2003 at 02:32PM PDT, ID: 8609281

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.29.2003 at 04:12PM PDT, ID: 8609764

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Pascal Programming Language
Tags: hanoi, recursive
Sign Up Now!
Solution Provided By: Okey
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32