473,287 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

explanation for towers of hanoi

50
hi guys
cud some1 explain to me the towers of hanoi code, the source code is available evrywhr. i've been trying to understand it bt cant. can some1 plz help me
i hav even tried to "trace" it but i still dnt understand hw it works plzzzz help

Expand|Select|Wrap|Line Numbers
  1. void Towers(int numDisks, char Source, char Dest, char Temp)
  2.     {
  3.         if (numDisks == 1)
  4.         {
  5.           cout << "\tMove top disk from pole " << Source << " to pole " << Dest << endl;
  6.         }
  7.         else
  8.         {
  9.           Towers(numDisks - 1, Source, Temp, Dest);
  10.           Towers(1, Source, Dest, Temp);
  11.           Towers(numDisks - 1, Temp, Dest, Source);
  12.         }
  13.     }
  14.  
  15.  
Oct 7 '07 #1
6 4603
JosAH
11,448 Expert 8TB
Suppose you have to move a tower of n disks from position 1 to position 3. The
problem is solved when you are able to move a tower of n-1 disks from position
1 to position 2; move the largest disk (disk number n) from position 1 to position
3 directly and then do the same with that tower of n-1 disks: move it from position
2 to position 3 (on top of that largest disk n).

Nothing needs to be done if n == 0.

kind regards,

Jos
Oct 7 '07 #2
poopsy
50
ok suppose i have 4 disks
when it 1st enter the function it goes to the else part, it comes to this part of code:

Expand|Select|Wrap|Line Numbers
  1. Towers(numDisks - 1, Source, Temp, Dest);
therefore numdisks-1= 3, source = position 1, temp= position 2, dest=position 3

wat happens then, does it continue to the next part of code which is :

Expand|Select|Wrap|Line Numbers
  1. Towers(1, Source, Dest, Temp);
but then numdisks is still not equal to 1!!

???
Oct 8 '07 #3
JosAH
11,448 Expert 8TB
That code you've shown us is not correct; use the following code instead:

Expand|Select|Wrap|Line Numbers
  1. void Towers(int numDisks, char Source, char Dest, char Temp)
  2.     {
  3.         if (numDisks > 0)
  4.         {
  5.           Towers(numDisks - 1, Source, Temp, Dest);
  6.           cout << "\tMove top disk from pole " << Source << " to pole " << Dest << endl;
  7.           Towers(numDisks - 1, Temp, Dest, Source);
  8.         }
  9.     }
  10.  
kind regards,

Jos
Oct 8 '07 #4
poopsy
50
if i want to show the positions of numdisks in each position after each pass, i.e i want to store it in an array, i'll have to declare 3 arrays for each of the 3 different positions where must i store it ( i.e whr in the code) and shud i display it after each call of the functions towers..plz guide me
Oct 8 '07 #6
Ganon11
3,652 Expert 2GB
If you want to display the positions, you'll want to do it once, before the solution is called, and then once for every move that's made (which will eventually include the solution). You'll also have to decide how you want to store it. You could declare three arrays large enough to hold the max number of disks, and then populate them with the disk numbers - but, with an original pile size of N, you'll only ever fill N out of 3N array slots, leaving 2N slots wasted.

Basically, printing isn't going to be your problem. How you keep track of what disk is where, and how you'll update this information, will be troublesome.
Oct 8 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Sonia | last post by:
Hi, I have written this code for the Tower of Hanoi problem. I was just wondering if there is any way of optimizing this code ? It's a failry simple code, but I thought that maybe somehow it...
8
by: Constant | last post by:
I need a programe that will deal with the solving of the problem ..i have 3 pegs A, B, C...and I want to move the disk A to disk C using B as auxiliary.At the end all disk should be at peg C in the...
7
by: ashishnh33 | last post by:
hi.......is there nyone to tell me how to solve the "tower of hanoi" problems
3
by: kamvisiouma | last post by:
Hi there, I'm fresh in learning c++ and I have to solve an exercise with hanoi and my problem is that I'm running out of time! The exercise is this, and I have to complete the ??? ...
51
by: maloov | last post by:
I have a pretty tough assignment for beginner like me & i'm seeking help please here is the assign In this assignment, you will be guided to complete the program skeleton provided to you in...
3
by: shaghayeghcute2003 | last post by:
I'm currently trying to write The Tower Of Hanoi (http:// illuminations.nctm.org/ActivityDetail.aspx?ID=40) in C#. I have a panel on the form and I draw pegs on the panel inside the form class. I...
1
by: schoenfeld.one | last post by:
In this article we show that "top-down" controlled demolition accurately accounts for the collapse times of the World Trade Center towers. A top-down controlled demolition can be simply...
5
by: seemanikam | last post by:
Can anyone suggest me how to devise an efficient divide-and-conquer algorithm for the Tower-of-Hanoi problem when the disks are colored alternately red and blue, and with the extra rule that no disk...
2
by: sunyboy | last post by:
hi I need a programe "Towers of Hanoi without use recessive function" in c++ Please help. Thanks for your time.
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.