answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<conio.h>

void main()

{

int *d,*s,i,n,dir;

clrscr();

printf("Enter the number of disk\n");

scanf("%d",&n);

dir=n&1;

for(i=0;i<=n+1;i++)

{

d[i]=1;s[i]=i+1;

}

for(;;)

{

i=s[0];

if(i>n)

break;

printf("Move disk %d from tower%d to tower%d\n",i,d[i]=(d[i]+(i&1?dir:1-dir))%3+1,d[i]);

s[0]=1;

s[i-1]=s[i];

s[i]=i+1;

}

getch();

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

#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;

const size_t max=9;

do

{

if( rings<1 rings>max )

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

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

string input;

getline(cin, input);

stringstream(input)>>rings;

cout<<endl;

}while(rings<1 rings>max);

// 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;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<string>

#include<stack>

#include<vector>

struct tower_t

{

std::string name;

std::stack<size_t> stack;

void move (tower_t& other);

};

std::ostream& operator<< (std::ostream& os, tower_t& t)

{

// Stacks do not have iterators therefore we must destroy

// the stack to create a vector, print the vector, then

// rebuild the stack.

os << t.name << ": ";

std::vector<size_t> v;

while (!t.stack.empty())

{

v.push_back (t.stack.top());

t.stack.pop();

}

size_t i=v.size();

while (i--)

os << v[i] << ' ';

while (!v.empty())

{

t.stack.push (v.back());

v.pop_back();

}

return os;

}

void tower_t::move (tower_t& other)

{

static size_t step = 0;

assert (stack.size());

assert (other.stack.size()==0 (stack.top() < other.stack.top()));

std::cout << "Step " << ++step <<

" move ring " << stack.top() <<

" from " << name <<

" to " << other.name <<

std::endl;

other.stack.push (stack.top());

stack.pop();

}

void move (size_t discs, tower_t& from, tower_t& to, tower_t& remain)

{

if (!discs) return;

move (discs-1, from, remain, to);

from.move (to);

move (discs-1, remain, to, from);

}

int main()

{

size_t rings = 5;

tower_t towers[3];

towers[0].name = "A";

towers[1].name = "B";

towers[2].name = "C";

for (size_t ring=rings; ring; --ring)

towers[0].stack.push (ring);

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

std::cout << towers[tower] << std::endl;

move (rings, towers[0], towers[2], towers[1]);

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

std::cout << towers[tower] << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Write a c programme for tower of honoi problem with a example of 4 disks.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c plus plus program of tower of hanoi?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Formula for tower of hanoi?

times 2 plus 1


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


Write a program in c plus plus to implement macro processor?

Don't write, it is already written, google for 'cpp'.


How do you write program to convert meter to kilometer in c plus plus?

Divide it by 1000.


Do I need to write a program to find a substring in a given string in c plus plus?

No.


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


Write a program in c plus plus to compute first of non-terminal?

there is no solution of this problem...........that's it..........


How many classes can we write in a single c plus plus program?

Its limited only by available memory.


How do you write a C plus plus program that displays a pyramid of Xes on the screen using a for loop?

printf ("x")


Write a program in C programming language that computes the roots of the quadratic equation ax2 plus bx plus c?

Write your program and if you are having a problem post it here with a description of the problem you are having. What you are asking is for someone to do your homework for you.


Write a c plus plus program to compute two integers?

int x= 1; int y= 2;