473,387 Members | 1,925 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,387 software developers and data experts.

Tower of Hanoi - Can it be optimized some how ?

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 could be
improved. Please let me know. Thanks
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
void move(int, int, int, int);

int count = 0;

int main() {

int numToBeMoved;
int peg1=1; // peg on which the disks are initially
int peg2=2; // peg to which the disks are to be moved
int peg3=3; // temporary holding area

cout<<"Enter number of disks to be moved: ";
cin>>numToBeMoved;

move(numToBeMoved, peg1, peg2, peg3);

cout<<"Number of moves: "<<count<<endl;
return 0;
}

void move (int n, int p1, int p2, int p3)
{

if(n>=1) {
move(n-1, p1, p3, p2);
count++;
cout<<"move from "<<p1<< " to " <<p2<<endl;
move(n-1, p3,p2,p1);
}
}
Jul 19 '05 #1
3 6177
If it can help, i wrote this game in PHP :
http://www.webjeff.org/jeux/index.htm

I can send you the source code if you want.
Jul 19 '05 #2
"Sonia" <sm*****@hotmail.com> schrieb im Newsbeitrag news:nO****************@bignews2.bellsouth.net...
: 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 could be
: improved. Please let me know. Thanks

There's not much to imporve on the algotizhem itself, but I have some suggestions on your source code. They may not seem to be very usefull in small programs like this, but when you have to work on a large project the hopefully will be usefull...

: #include <iostream>
:
: using std::cout;
: using std::cin;
: using std::endl;
:
: void move(int, int, int, int);

Even though it is allowed by the language, don't omit parameter names from prototypes. When you later see the prototype only, you will probably not remember which of the four ints has which meaning. (Not to mention others reading your code.)

void move(int numberOfDisks, int sourcePeg, int targetPeg, int intermediatePeg);

Reading a prototype like this you will not have to look for more documentation. Most of the information you need is already there.

: int count = 0;

Avoid global variables whenever possible. If you have to use one, give it a descriptive name that is not likely to be used for anything else. count is too common a name to be used for a global variable. Change it to something like numberOfMovesMade, or even better, change move() to return the number of moves made.

: int main() {
:
: int numToBeMoved;
: int peg1=1; // peg on which the disks are initially
: int peg2=2; // peg to which the disks are to be moved
: int peg3=3; // temporary holding area

peg1, peg2 and peg3 never change, so they should be const. Also, these names are not really descriptive. You even have to write comments to describe their meanings. Whenever you have to do that, try to find a better name, for example

const int initialPeg = 1;
const int targetPeg = 2;
const int holdingArea = 3;

: cout<<"Enter number of disks to be moved: ";
: cin>>numToBeMoved;
:
: move(numToBeMoved, peg1, peg2, peg3);
:
: cout<<"Number of moves: "<<count<<endl;
: return 0;
: }
:
: void move (int n, int p1, int p2, int p3)

Again, names like p1, p2 and p3 are not very helpfull in most cases. If you don't like to write the long names from the prototype, use short but still descriptive names like from, to and hold.

: {
:
: if(n>=1) {

You might improve the algorithm, if you replaces the condition by n>1 and add a special case for n==1. That will save about have the calls to move(), namely all those calls where n==0 and move() does nothing at all. It might also be an improvement to compute n-1 once, but a good compiler should be able to optimize that itself.

: move(n-1, p1, p3, p2);
: count++;
: cout<<"move from "<<p1<< " to " <<p2<<endl;
: move(n-1, p3,p2,p1);
: }
: }

Finally the move function with all the suggested changes:

int move(int n, int from, int to, int hold)
{
int count = 1;
if (n > 1)
{
count += move(n - 1, from, hold, to);
cout << "move from " << from << " to " << to << endl;
count += move(n - 1, hold, to, from);
}
else
{
cout << "move from " << from << " to " << to << endl;
}
return count;
}

Regards
Heinz
Jul 19 '05 #3

"Heinz Ozwirk" <wa******@gmx.de> wrote in message
news:bi*************@news.t-online.com...
"Sonia" <sm*****@hotmail.com> schrieb im Newsbeitrag
news:nO****************@bignews2.bellsouth.net...
: 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 could be
: improved. Please let me know. Thanks

There's not much to imporve on the algotizhem itself, but I have some
suggestions on your source code. They may not seem to be very usefull in
small programs like this, but when you have to work on a large project the
hopefully will be usefull...

: #include <iostream>
:
: using std::cout;
: using std::cin;
: using std::endl;
:
: void move(int, int, int, int);

Even though it is allowed by the language, don't omit parameter names from
prototypes. When you later see the prototype only, you will probably not
remember which of the four ints has which meaning. (Not to mention others
reading your code.)

void move(int numberOfDisks, int sourcePeg, int targetPeg, int
intermediatePeg);

Reading a prototype like this you will not have to look for more
documentation. Most of the information you need is already there.

: int count = 0;

Avoid global variables whenever possible. If you have to use one, give it a
descriptive name that is not likely to be used for anything else. count is
too common a name to be used for a global variable. Change it to something
like numberOfMovesMade, or even better, change move() to return the number
of moves made.

: int main() {
:
: int numToBeMoved;
: int peg1=1; // peg on which the disks are initially
: int peg2=2; // peg to which the disks are to be moved
: int peg3=3; // temporary holding area

peg1, peg2 and peg3 never change, so they should be const. Also, these names
are not really descriptive. You even have to write comments to describe
their meanings. Whenever you have to do that, try to find a better name, for
example

const int initialPeg = 1;
const int targetPeg = 2;
const int holdingArea = 3;

: cout<<"Enter number of disks to be moved: ";
: cin>>numToBeMoved;
:
: move(numToBeMoved, peg1, peg2, peg3);
:
: cout<<"Number of moves: "<<count<<endl;
: return 0;
: }
:
: void move (int n, int p1, int p2, int p3)

Again, names like p1, p2 and p3 are not very helpfull in most cases. If you
don't like to write the long names from the prototype, use short but still
descriptive names like from, to and hold.

: {
:
: if(n>=1) {

You might improve the algorithm, if you replaces the condition by n>1 and
add a special case for n==1. That will save about have the calls to move(),
namely all those calls where n==0 and move() does nothing at all. It might
also be an improvement to compute n-1 once, but a good compiler should be
able to optimize that itself.

: move(n-1, p1, p3, p2);
: count++;
: cout<<"move from "<<p1<< " to " <<p2<<endl;
: move(n-1, p3,p2,p1);
: }
: }

Finally the move function with all the suggested changes:

int move(int n, int from, int to, int hold)
{
int count = 1;
if (n > 1)
{
count += move(n - 1, from, hold, to);
cout << "move from " << from << " to " << to << endl;
count += move(n - 1, hold, to, from);
}
else
{
cout << "move from " << from << " to " << to << endl;
}
return count;
}

Regards
Heinz

Thank Heinz, Helped a lot.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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 ??? ...
7
by: bonk | last post by:
I have a c# project as part of a larger VS 2005 solution that always gets build optimized and I therefore can not evaluate any values while debugging through the code ("Cannot evaluate expression...
0
by: vaj | last post by:
Desktop Tower defense the most addictive flash game ever http://www.yelp.com/redir?url=http://www.teenwag.com/show/games
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...
6
by: poopsy | last post by:
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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.