Answered step by step
Verified Expert Solution
Question
1 Approved Answer
* PLEASE READ INSTRUCTIONS CAREFULLY, DO NOT CREATE ANY FUNCTIONS NOT GIVEN IN INSTRUCTIONS * In this project, we will implement a class that can
PLEASE READ INSTRUCTIONS CAREFULLY, DO NOT CREATE ANY FUNCTIONS NOT GIVEN IN INSTRUCTIONS In this project, we will implement a class that can solve a threepole Tower of Hanoi puzzle for any number of discs starting from any source pole and going to any other destination pole. First, our class will do so recursively, and then it will do so recursively and efficiently.
TODO
Stage :
Implement the recursive solution.
Commit and push your work as you do so committing at a minimum after each function definition.
Test drive your recursive implementation from srcmaincpp confirming correct solutions.
Commit and push your work.
Iterate as necessary, and commit and push your work throughout.
Commit and push successful program runs for stage
Stage :
Memoize and optimize your solution.
Commit and push your work.
Test drive your memoized implementation from srcmaincpp
Commit and push your work.
Run unit tests.
Iterate as necessary, and commit and push your work throughout.
Commit and push successful program runs for stage Solution output will of course be identical to stage
Add a "Project Complete" comment to your Feedback pull request.
Give yourself yet another high five!
make terminal commands
make
builds binmain executable
builds testsbintest executables
make main
builds binmain executable
make runmain
builds and runs binmain executable
make memcheckmain
runs binmain executable with Valgrind:
valgrind leakcheckyes binmain
make tests
builds testsbintest executables
make runtests
builds and iteratively runs all testsbintest executables
Note: VSCode test explorer can be used as an alternative to run tests after they're built
make runtestsverbose
builds and iteratively runs all testsbintest executables with verbose output
Note: VSCode test explorer can be used as an alternative to run tests after they're built
make memchecktests
builds and iteratively runs all testsbintest executables with Valgrind:
valgrind leakcheckyes testsbintestnametest
make clean
removes all build artifacts
Implementation Notes & Details
Stage
std::string Hanoi::solveint numdiscs, int src int dst int tmp
This is the one and only public member function of the Hanoi class. It represents the user interface.
The user will call solve and provide the number of discs numdiscs they would like to move from the source pole src to the destination pole dst using the third pole as a temporary pole tmp
solve returns a string representation of the necessary moves preceded by the following line:
# Below, AB means 'move the top disc on pole A to pole B
solve will supply this line and then add to it the string solution provided by calling getmoves
For example, calling solve would ultimately return the string:
# Below, AB means 'move the top disc on pole A to pole B
std::string Hanoi::getmovesint numdiscs, int src int dst int tmp
getmoves returns the string representation of the required moves to move numdiscs discs from the src pole to the dst pole using the tmp pole as a temporary pole.
Each move will be on its own line.
For example, getmoves would return the string:
Stage
Hanoi::cache
cache is a private data member of the Hanoi class.
You must declare this data structure in hanoi.h
cache is a dimensional vector that ultimately holds string representations for the sequence of moves that represent a solution for moving numdiscs the first dimension of cache from pole src the second dimension of cache to pole dst the third dimension of cache
That makes cache a vector of vector of vector of strings.
For example, accessing the cache like so: cache would give you the string representation for the sequence of moves necessary to move discs from pole to pole using pole as a temporary pole if that solution was available in the cache.
std::string Hanoi::lookupmovesint numdiscs, int src int dst const
lookupmoves is responsible for accessing cache.
If the requested string solution is found, return it
Otherwise return an empty string.
Memoization
cache
Declare the cache data structure in hanoi.h
Implement lookupmoves
Update getmoves to use cache.
When invoked, first check the cache for the solution.
If the cache entry exists, return it
Otherwise, calculate the sequence of moves as before, but store it in the cache before returning.
Only create cache entries lazily. That is don't create cachei or cacheij or cacheijk until you actually have a use for it In other words, you will only be expanding your cache dimensions as necessary and on demand so as to keep the memory footprint as light as possible.
Use pole labels and not and or any other This means that cachei and cacheij are dead locations you won't use in your program. But the savings you get in reduced cognitive load.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started