Answered step by step
Verified Expert Solution
Link Copied!

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 three-pole 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 1:
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 src/main.cpp confirming correct solutions.
Commit and push your work.
Iterate as necessary, and commit and push your work throughout.
Commit and push successful program run(s) for stage 1.
Stage 2:
Memoize and optimize your solution.
Commit and push your work.
Test drive your memoized implementation from src/main.cpp.
Commit and push your work.
Run unit tests.
Iterate as necessary, and commit and push your work throughout.
Commit and push successful program run(s) for stage 2. Solution output will of course be identical to stage 1.
Add a "Project 02 Complete" comment to your Feedback pull request.
Give yourself yet another high five!
make terminal commands
make
builds bin/main executable
builds tests/bin/*.test executables
make main
builds bin/main executable
make run_main
builds and runs bin/main executable
make memcheck_main
runs bin/main executable with Valgrind:
valgrind --leak-check=yes ./bin/main
make tests
builds tests/bin/*.test executables
make run_tests
builds and iteratively runs all tests/bin/*.test executables
Note: VSCode test explorer can be used as an alternative to run tests after they're built
make run_tests_verbose
builds and iteratively runs all tests/bin/*.test executables with verbose output
Note: VSCode test explorer can be used as an alternative to run tests after they're built
make memcheck_tests
builds and iteratively runs all tests/bin/*.test executables with Valgrind:
valgrind --leak-check=yes ./tests/bin/[test_name].test
make clean
removes all build artifacts
Implementation Notes & Details
Stage 1
std::string Hanoi::solve(int num_discs, 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 (num_discs ) 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, 'A->B' 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 get_moves().
For example, calling solve(2,1,2,3) would ultimately return the string:
# Below, 'A->B' means 'move the top disc on pole A to pole B
1->3
1->2
3->2
std::string Hanoi::get_moves(int num_discs, int src, int dst, int tmp)
get_moves() returns the string representation of the required moves to move num_discs 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, get_moves(2,1,2,3) would return the string:
1->3
1->2
3->2
Stage 2
Hanoi::_cache
_cache is a private data member of the Hanoi class.
You must declare this data structure in hanoi.h.
_cache is a 3-dimensional vector that ultimately holds string representations for the sequence of moves that represent a solution for moving num_discs (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[4][1][3] would give you the string representation for the sequence of moves necessary to move 4 discs from pole 1 to pole 3 using pole 2 as a temporary pole -- if that solution was available in the cache.
std::string Hanoi::lookup_moves(int num_discs, int src, int dst) const
lookup_moves() 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 lookup_moves()
Update get_moves() 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 _cache[i] or _cache[i][j] or _cache[i][j][k] 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 1,2 and 3(not 0,1 and 2 or any other). This means that _cache[i][0] and _cache[i][j][0] are dead locations you won't use in your program. But the savings you get in reduced cognitive load.|
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 15th International Conference Dasfaa 2010 International Workshops Gdm Benchmarx Mcis Snsmw Diew Udm Tsukuba Japan April 2010 Revised Selected Papers Lncs 6193

Authors: Masatoshi Yoshikawa ,Xiaofeng Meng ,Takayuki Yumoto ,Qiang Ma ,Lifeng Sun ,Chiemi Watanabe

2010th Edition

3642145884, 978-3642145889

More Books

Students also viewed these Databases questions