Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following was done in C++ Problem: I need to solve the Towers of Hanoi game for the following graph G=(V,E) with V={Start, Aux1, Aux2,

The following was done in C++

Problem: I need to solve the Towers of Hanoi game for the following graph G=(V,E) with V={Start, Aux1, Aux2, Aux3, Aux4, Dest} and E = {(Start, Aux1), (Aux1, Aux2), (Aux2, Aux3), (Aux3, Aux4), (Aux4, Aux1), (Aux1, Dest)}.

a. I need to design an algorithm and determine the time and space complexities of moving n disks from Start to Dest.

b. Implement the algorithm whereby my program prints out each of the moves of every disk. I need to show the output for n=1,2,3,4,5,6,7,8,9,10. If the output is too long, then print out only the first 100 and the last 100 moves (which I assume will happen)

Please make needed corrections to my code so that it may compile/debug

//Towers of Hanoi Game #include using namespace std;

int main() { this_in = new Scanner(system_in); this_current = 1; cout << "Enter the number of disks: "); //asks for number of disks this_n = in_nextInt(); this_hasItMoved = new int[12]; this.toDest = new int[12]; for (int j = 0; j < this_toDest_length; j++) this_toDest[j] = 1; for (int i = 1; i <= n; i++) this_toDest[i] = 0; r = n; hanoiStart(n, 'Start', 'Aux1', 'Aux3', 'Aux2', 'Dest', this_current); //Initializing the 5 pegs }

//recursive function to move the disks around the pegs

{ move(1, Start, source, this_current); this_current++; // call the move function H1(n, 'Start', 'Aux1', 'Aux3', 'Aux2', 'Dest', this_current); //recursive function call move(1, dest, last, this_current); this_current++; //call the move function }

int H1(int numOfDisks, String Start, String source, String dest, String aux, String last, int current)

{ if (n == 1) { cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl; return; } towerOfHanoi(n, from_rod, aux_rod, to_rod); cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; towerOfHanoi(n, aux_rod, to_rod, from_rod); }

else if (n == 2)

{ cout << "Move disk 2 from rod " << from_rod << " to rod " << to_rod << endl; return; } towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); if (this_current == 4) { move(n, from_rod, aux_rod, to_rod; to_rod++; } move(n, from_rod, dest_rod, aux_rod, to_rod; to_rod); to_rod++; move(n - 1, from_rod, aux_rod, to_rod; to_rod); to_rod++; move(n - 1, from_rod, aux_rod, source_rod, to_rod; to_rod); to_rod++; move(n, from_rod, dest_rod, aux_rod, to_rod; to_rod); to_rod++; if (this r == 2) move(2, dest_rod, to_rod); to_rod; move(n - 1, from_rod, aux_rod, to_rod; to_rod); to_rod++; move(n - 1, from_rod, aux_rod, dest_rod, to_rod; to_rod); to_rod++; }

//scenario if the num of disks is greater than 2

else if (n > 2) { this_current = H1(n - 1, Start, from_rod, source_rod, dest_rod, aux_rod, to_rod); if (move((this_rod)) then(n != 1) { move(n, from_rod, source_rod, to_rod); to_rod++; to_rod(move(n = 1)); } move(n, source_rod, aux_rod to_rod); to_rod++; to_rod = H1(n - 1, from_rod, dest_rod, source_rod, aux_rod, to_rod, to_rod); move(n, aux_rod, dest_rod, to_rod); to_rod++; if (dest_rod[n + 1] != 0) { move(n, dest_rod, last_rod, last_rod); to_rod++; dest_rod[n] = 1; } if (n == r) this == r; current = H1(n - 1, start_rod, source_rod, dest_rod, aux_rod, last_rod, last_rod); }; return 0; } void move(int aDisk, String source, String source, String dest, int currentStep) { cout << "Move " this_rod" << ": Move disk " << aDisk << " from " << source_rod << " to " << dest); }" }

// Driver code

int main() { int n = 10; // Number of disks towerOfHanoi(n, 'Start', 'Aux1', 'Aux3', 'Aux2', 'Dest', this.current); return 0; }

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_2

Step: 3

blur-text-image_3

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

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Eric Redmond ,Jim Wilson

1st Edition

1934356921, 978-1934356920

More Books

Students also viewed these Databases questions