Thursday, 6 March 2014

Tower Of Hanoi Using Backtracking in c++

#include<iostream.h>

#include<conio.h> 

void tower(int,char,char,char); 

void main()

{

int n; 

clrscr();

cout<<"enter the disk number :  ";

cin>>n;tower(n,'A','C','B'); 

getch();

void tower(int n,char from,char to,char aux)

{

if(n==1)
    {

    cout<<endl<<"move 1 from peg "<<from<<" to "<<to;return;

   }

tower(n-1,from,aux,to);

cout<<endl<<"move "<<n<<" from peg "<<from<<" to "<<to;

tower(n-1,aux,to,from);

}


No comments:

Post a Comment