answersLogoWhite

0

Tower of Hanoi C plus plus visual?

Updated: 10/25/2022
User Avatar

Wiki User

10y ago

Best Answer

#include<iostream>

#include<string>

#include<vector>

#include<sstream>

using namespace std; // For std::cout and std::endl

// Each rod in the tower has a variable number of rings.

typedef vector<size_t> Rod;

// Displays the tower.

void display(Rod tower[], size_t move=0)

{

if(!move)

cout<<"Initial Position";

else

cout<<"Move "<<move;

cout<<":\n"<<endl;

static const char label[]="ABC";

for(size_t rod=0; rod<3; ++rod)

{

cout<<label[rod]<<":";

for(size_t ring=0; ring<tower[rod].size(); ++ring)

cout<<" "<<tower[rod][ring];

cout<<endl;

}

cout<<endl;

}

int main()

{

cout<<"Tower of Hannoi\n===============\n"<<endl;

size_t rings=1;

do

{

if( rings<1 rings>9 )

cout<<"You must enter a value in the range 1..9.\n"<<endl;

cout<<"Enter the no. of rings (1..9): ";

string input;

getline(cin, input);

stringstream(input)>>rings;

cout<<endl;

}while(rings<1 rings>9);

// Instantiate the three towers.

Rod tower[3];

// Push all the rings onto the 1st tower (largest to smallest)

// and display the initial position.

for(size_t ring=0; ring<rings; ++ring)

tower[0].push_back(rings-ring);

display(tower);

// Determine the minimum no. of moves required

// (2 raised to the power of rings, minus 1).

size_t moves=(1<<rings)-1;

// Determine if the number of rings is odd or even.

size_t odd=rings&1;

// Begin moving rings...

size_t move=0;

while(move<moves)

{

// Determine which 2 of the 3 rods to compare.

size_t rod_index1=0, rod_index2=odd?1:2;

switch(move%3)

{

case(0): rod_index2=odd?2:1; break;

case(2): rod_index1=odd?2:1; break;

}

// Reference the two rods.

Rod& rod1=tower[rod_index1];

Rod& rod2=tower[rod_index2];

// Obtain the top-most ring sizes (0 if rod is empty).

size_t ring1=rod1.size()?*rod1.rbegin():0;

size_t ring2=rod2.size()?*rod2.rbegin():0;

// Invariant: ring1 + ring2 > 0.

// Move the smallest ring to the other rod.

if(!ring2 (ring1 && ring1<ring2))

{

rod1.pop_back();

rod2.push_back(ring1);

}

else

{

rod2.pop_back();

rod1.push_back(ring2);

}

// Display the result of the move.

display(tower, ++move);

}

// Finished!

cout<<"Complete in "<<moves<<" moves!\n"<<endl;

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Tower of Hanoi C plus plus visual?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Who developed visual c plus plus?

Microsoft developed Visual C++.


What is the brief history of visual c plus plus?

The complete history of Visual C++ can be found in the "Visual C++" article in Wikipedia.


Is Visual C or Visual C plus plus platform independent?

No, M$ Windoze only.


Is microsoft visual c plus plus the same as visual c plus plus?

Yes. Microsoft Visual C++ is the correct name, but it is often abbreviated to MSVC++ or just VC++. They are all the same.


Can you make programs of C in visual c plus plus?

Yes.


Is visual studio c plus plus 2010 free?

No. Visual C++ Express is free, but Visual Studio C++ must be bought. The Express edition is a subset of the Studio release.


C program to implement tower of hanoi using array implementation of stack abstract datatype?

stack abstract datatype


How do you solve the c programme of tree of hanoi?

It is the "Tower of Hanoi" and it is a puzzle. When you can solve the puzzle you can write the program, when you have learned enough to write simple programs. You do want to be able to write computer programs, don't you?


How can you make a triangle in Visual C plus plus 2010?

Nevermind, I did it.


Visual c plus plus 6.0 free installer?

Visual C++ 6.0 is more than 16 years old. It is no longer available to buy and it was never available for free. The current version is Visual C++ 2013.


Which c plus plus best for vista?

Microsoft Visual C++ or Embarcadero C++ Builder (formally Borland C++ Builder).


What has the author Chuck Sphar written?

Chuck Sphar has written: 'Learn Microsoft Visual C++ 6.0 now' -- subject(s): C++ (Computer program language), Microsoft Visual C++ 'Learn Microsoft Visual C [plus plus] 6.0 now' -- subject(s): C