Towers Of Hanoi
I'm looking to create a version of the url=http://en.wikipedia.org/wiki/Tower_of_Hanoi]towers of hanoi[/url] problem using C++. If your unfamiliar with the game, it involves moving hoops from one peg to another, without placing larger hoops on top of smaller hoops.
I am not looking for a program that will solve it automatically.
But I am having difficulty on deciding the best way to handle the logic behind the problem. I am going to do it with 3 hoops at first. But I need to decide how to represent these hoops being on each peg.
I was thinking of using 3 instances of a class something like this:
But thought that this could lead to far too many conditional statements for all the possible moves.
bool hoopA, hoopB, hoopC;
} Peg1, Peg2, Peg3;
Data structures are typically dictated by the algorithm. Do you know how you plan on solving the problem? As Wikipedia will tell you, there are a few ways of analyzing it.
One of the more intuitive ways of starting would be to create a stack for each peg.
It will be for the user to solve. Stacks appear to be exactly what I'm looking for. Thanks for the help :)
Yes use three stacks. You can only move a hoop if the source stack is non-empty, and the destination stack is either empty or the top hoop on it has a larger diameter than the hoop from the source stack.
The stacks themselves need only store ints, where each int is the hoop diameter.
Hope that helps.