Active1 year, 3 months ago
I am working on an exercise in a book which asks us to solve the Towers of Hanoi problem using recursive methods. I have come to a solution, but from what I gather after browsing the Internet when done is that my solution may not be correct. Does anyone know a better/different way to solve the problem? And does anyone have nay suggestions for improvements. (Btw, the out put is correct. It is only supposed to tell from which tower to another pegs are moving, not specifically which pegs)
This video tutorial explain you tower of hanoi problem. This is a very famous game. In this game there are three pegs and n number of disks. You need to move. IT IS A COMPUTER GRAPHICS PROJECT MADE USING VISUAL STUDIO. IN THIS PROJECT IT SHOWS ABOUT TOWER OF HANOI. Programming in Visual C++ Using OpenGL, Introduces the three-dimensional computer graphics with OpenGL. IT IS A COMPUTER GRAPHICS PROJECT MADE USING VISUAL STUDIO. IN THIS PROJECT IT SHOWS ABOUT TOWER OF HANOI. Programming in Visual C++ Using OpenGL, Introduces the three-dimensional computer graphics with OpenGL. My problem I want design tower of Hanoi game with C++ using stack But i do not know how can. Basics without using any graphics. Of towers of hanoi. The Tower of Hanoi (also called the Tower of Brahma or Lucas’ Tower, and sometimes pluralized). Program to solve Tower of Hanoi C++ using Stack.
Here is the code:
Is this method qualified as recursion?
E.O.E.O.
36455 gold badges1212 silver badges2323 bronze badges
5 Answers
To answer your question: yes, that is qualified as recursion. Any time a function calls itself, it is recursion.
With that being said, your code can be trimmed down substantially:
riwalkriwalk12.4k55 gold badges4040 silver badges6565 bronze badges
It's easiest if you look at the problem recursively:
To move N discs from A to B (using C):
- if (N > 1) move N-1 discs from A to C (using B)
- move one disc from A to B
- if (N > 1) move N-1 discs from C to B (using A)
For any given call, whichever peg is not the source or the destination is the ancillary.
To answer your actual question: yes, your solution appears to be recursive, though a bit more complex than really necessary.
Jerry CoffinJerry Coffin398k5858 gold badges497497 silver badges940940 bronze badges
Every recursion method has 3 steps
1) A check condition2) Return value when check condition is satisfied.3) A call to method itself
@Stargazer712 solution is perfect.
Tower Of Hanoi C++ Stacks
NickNick1,21033 gold badges1616 silver badges2929 bronze badges
Umair A R MughalUmair A R Mughal
isaac kargarisaac kargar
Not the answer you're looking for? Browse other questions tagged c++recursiontowers-of-hanoi or ask your own question.
Featured Topics
- LTT Official Folding Month 2019!!!By GOTSpectrum
Started
Topics
- By MooseParade
Started
Posted in Laptops and Pre-Built Systems - By stefs
Started
Posted in Networking - By InfinityVive
Started
Posted in General Discussion - By prostrike33
Started
Posted in Storage Devices - By Brass_01
Started
Posted in General Discussion - By InfinityVive
Started
Posted in PC Gaming - By Stargaz3r
Started
Posted in Displays - By Succumb
Started
Posted in Cases and Power Supplies - By Gerjan
Started
Posted in New Builds and Planning - By rmog2133
Started
Posted in Networking
- play_circle_filled
Latest From Linus Tech Tips:
The CHEAPEST 144Hz Gaming Monitors on Amazon!
- play_circle_filled
Latest From Tech Quickie:
Is Overclocking Worth It?
- play_circle_filled
Latest From TechLinked:
Intel's getting desperate...