473,396 Members | 2,059 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,396 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 4605
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.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.