answersLogoWhite

0


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: What a c plus plus program that solve the tower of hanoi problem of n disk?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the minimum amount of moves for 64 disks on tower of hanoi?

2^64-1 = 18446744073709551615


Move 5 disks in the tower of hanoi algorithm?

/* tower of hanoi using recursion */ #include&lt;stdio.h&gt; int main(void) { unsigned int nvalue; char snvalue = 'L' , invalue = 'C' , dnvalue = 'R' ; void hanoi(unsigned int , char , char , char); printf(" enter number of disks : "); scanf("%u",&amp;nvalue ); printf("\n\ntower of hanoi problem with %d disks \n ", nvalue )" hanoi(nvalue , snvalue , invalue , dnvalue ); printf("\n"); return 0 ; } void hanoi(unsigned n , char snd1 , char ind1 , char dnd1 ) { if(n!=0) { /* move n-1 disks from starting to intermadiate needles */ hanoi(n-1 , snd1 , dnd1 , ind1 ); /* move disk n from start to destination */ printf("move disk %d from %c to %c\n ", n , snd1 , dnd1); /* move n-1 disks from intermediate to destination needle */ hanoi(n-1 , ind1 , snd1 , dnd1 ); } }


Write a c program to implement tower of hanoi moves?

/* hanoi.c */ #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; static long step; static void Hanoi (int n, int from, int to,int spare) { if (n&gt;1) Hanoi (n-1,from,spare,to); printf ("Step %ld: move #%d %d--&gt;%d\n", ++step, n, from, to); if (n&gt;1) Hanoi (n-1,spare,to,from); } int main (int argc, char **argv) { int n; if (argc==1 (n= atoi(argv[1]))&lt;=0) n= 5; step= 0; Hanoi (n, 1, 2, 3); return 0; }


Towers of Hanoi code in c language using stacks?

#include &lt;iostream.h&gt; // a disk with a value , which is an element of the stack ,tower in this case class Disk { public: int value; Disk* next; }; class Tower //a stack data structure representing a tower { public: int size; Disk* current; Tower() { size=0; current=NULL; }//default constructor int peep(); bool push(int); bool pop(); bool isEmpty(); int getTowerSize(); void printTowerSize(); void printTowerDisks(); void printTowerMenu(); }; int Tower::peep() { return this-&gt;current-&gt;value; } bool Tower::push(int ele) { Disk* temp; temp=new Disk; if(current==NULL) { temp-&gt;next=NULL; } else { temp-&gt;next=current; } temp-&gt;value=ele; this-&gt;current=temp; size++; return false; } bool Tower::pop() { if(isEmpty()) { cout&lt;&lt;"\nTower is Empty\n"; return false; } else { current=current-&gt;next; size=size--; } return true; } bool Tower::isEmpty() { if(getTowerSize()==0) return true; return false; } int Tower::getTowerSize() { return size; }//returns size of the Tower void Tower::printTowerSize() { cout&lt;&lt;"\nThe Size of the Tower:"&lt;&lt;size&lt;&lt;"\n"; }//print the Tower size void Tower::printTowerDisks() { if(this-&gt;isEmpty()) { cout&lt;&lt;"-----\n"; cout&lt;&lt;" "&lt;&lt;endl; cout&lt;&lt;"-----\n"; return; } Disk *curr2; curr2=this-&gt;current ; cout&lt;&lt;"-----\n"; cout&lt;&lt;"Tower\n"; cout&lt;&lt;"-----\n"; int i=0; while(curr2 !=NULL) { if(i&gt;4) break; i++; cout&lt;&lt;" |"&lt;&lt;curr2-&gt;value&lt;&lt;"|\n"; curr2=curr2-&gt;next; } }// print the Tower void createSourceTower(Tower *source,int numberOfDisks) { for(int i=numberOfDisks;i&gt;0;i--) { source-&gt;push(i); } } void moveDisk(Tower *source,Tower *dest) // movinng a disk from source to destionation { dest-&gt;push(source-&gt;current-&gt;value ); source-&gt;pop(); } void hanoi( int N, Tower *source, Tower *dest,Tower *aux ) // move N disks from source to destination { if (N &gt; 0 ) { hanoi(N - 1, source, aux, dest); //move n-1 disks from source to auxxilary (sub problem) moveDisk(source,dest); //move nTH disk from source to destination hanoi(N - 1, aux, dest, source); //move n-1 disks from auxillary to destination (sub problem) } } void main() { Tower *source,*destination,*auxillary; //Towers required for the 3 towers source destination and auxillary source=new Tower; destination=new Tower; auxillary=new Tower; //take number of disks from user int numberOfDisks; cout&lt;&lt;"Enter number of Disks in the source Tower"; cin&gt;&gt;numberOfDisks; //inserting the disks into the source tower createSourceTower(source,numberOfDisks); cout&lt;&lt;"==============================================="&lt;&lt;endl; cout&lt;&lt;"Initial Scenario of the Towers "&lt;&lt;endl; cout&lt;&lt;"Source"&lt;&lt;endl; source-&gt;printTowerDisks (); cout&lt;&lt;"Auxillary"&lt;&lt;endl; auxillary-&gt;printTowerDisks (); cout&lt;&lt;"Destination"&lt;&lt;endl; destination-&gt;printTowerDisks (); hanoi( numberOfDisks,source, destination, auxillary ); cout&lt;&lt;"==============================================="&lt;&lt;endl; cout&lt;&lt;"Final Scenario of the Towers "&lt;&lt;endl; cout&lt;&lt;"Source"&lt;&lt;endl; source-&gt;printTowerDisks(); cout&lt;&lt;"Auxillary"&lt;&lt;endl; auxillary-&gt;printTowerDisks (); cout&lt;&lt;"Destination"&lt;&lt;endl; destination-&gt;printTowerDisks (); cout&lt;&lt;"==============================================="&lt;&lt;endl; }


How can you increase the cooling tower efficiency?

Pls do the followings; 1. Clean all the dust,debris,deposits,etc if any in tower structure periodically. 2. Increase the tower height if possible 3. Choose correct water treatment program

Related questions

How can you solve the tower of hanoi?

1. Write mathematical analysis for recursive algorithms. Solve Tower of Hanoi Problem and analyze it.


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?


Grey humpreys tower of hanoi problem step by step explanation?

Can I please have the steps, sketch and formula for the tower of hanoi


How do you solve the tower of Hanoi?

There are a couple patterns that solve the puzzle. I am not sure of those patterns though.


The least number of moves in the tower of hanoi puzzle with five disks?

The number of moves required to solve the Hanoi tower is 2m + 1 . Therefore for a tower of five disks the minimum number of moves required is: 31.


Least number of moves in the tower of hanoi puzzle with five disks?

The number of moves required to solve the Hanoi tower is 2m + 1 . Therefore for a tower of five disks the minimum number of moves required is: 31.


When was the tower of hanoi invented?

The tower of hanoi was invented in 1883.........


What did edouard lucas discovered in 1883?

Tower of Hanoi puzzle game - mathematical problem


When was Flag Tower of Hanoi created?

Flag Tower of Hanoi was created in 1812.


Which country is Keangnam Hanoi Landmark Tower found?

Keangnam Hanoi Landmark Tower is a building found at Hanoi in Vietnam.


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

stack abstract datatype


What is the floor area of Keangnam Hanoi Landmark Tower?

The floor area of Keangnam Hanoi Landmark Tower is 609,673 m2.