answersLogoWhite

0

/* tower of hanoi using recursion */

#include<stdio.h>

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",&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 );

}

}

User Avatar

Wiki User

15y ago

What else can I help you with?

Continue Learning about Engineering

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


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


How would you design an algorithm for reversing two adjacent entries on a stack if you were given three stacks and you were only allowed to move entries one at a time from one stack to another?

Research Towers Of Hanoi http://en.wikipedia.org/wiki/Tower_of_Hanoi You will find your answer


An algorithm to Reversing the order of elements on stack S using 1 additional stacks?

// stack to contain content Stack sourceStack = new Stack(); // ... fill sourceStack with content // stack to contain reversed content Stack targetStack = new Stack(); while (!sourceStack.empty()) { targetStack.push(sourceStack.pop()); } // targetStack contains the reversed content of sourceStack


What is the suitable search method for the problem of 'Tower of Hanoi' in Artificial Intelligence?

Many search algorithms are possible. Tree-based methods, in which all paths to all solutions are produced, is one option. Each node in the tree would represent a "state" or "configuration" of the problem, while an edge from one node to the next represents the "move" you make. Consequently, finding a solution to this problem is equivalent to building the tree while checking if each node is a valid solution. Another method, such the A* algorithm is a heuristic search algorithm. You would use a heuristic function that estimates the optimal path to the solution from the current node. It is the quickest, but since it is a heuristic algorithm, it is not guaranteed to always return the correct answer, since this is dependent on the heuristic function you use in your algorithm.

Related Questions

What is the least number of moves in the Tower of Hanoi puzzle with only 5 disks?

To move n disks, you need 2n-1moves. In this case, 31.


Algorithm for tower of hanoi using recursion?

move from, to, spare, count: move from, spare, count-1 single_move from, to move spare, to, count-1


According to the tower of hanoi in how many years will the world end?

According to the legend, when the last move of the Tower of Hanoi puzzle is completed, the world will end.


What is the game where you move rings on to a different post without putting larger disks on to smaller disks?

That game with 5 or more rings and 3 posts is known as "the Towers of Hanoi".


Write an Algorithm for towers of hanoi?

#include#includevoid hanoi(int x, char from,char to,char aux){if(x==1){printf("Move Disk From %c to %c\n",from,to);}else{hanoi(x-1,from,aux,to);printf("Move Disk From %c to %c\n",from,to);hanoi(x-1,aux,to,from);}}int main(void){int disk;clrscr();printf("Enter the number of disks you want to play with:");scanf("%d",&disk);double moves=pow(2,disk)-1;printf("\nThe No of moves required is=%g \n",moves);hanoi(disk,'A','C','B');getch();}


How can one successfully solve the Tower of Hanoi puzzle and emerge victorious"?

To successfully solve the Tower of Hanoi puzzle and emerge victorious, one must follow a specific strategy of moving the disks from one peg to another while adhering to the rules of the game. The key is to always move the smallest disk first and to plan ahead to minimize the number of moves required. By carefully strategizing and being patient, one can solve the puzzle and achieve victory.


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


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


A c program of tower of honai?

void Hanoi (int n, int nfrom, int nto, int nspare) { if (n&gt;1) Hanoi (n-1, nfrom, nspare, nto); printf ("Move from %d to %d\n", nfrom, nto); if (n&gt;1) Hanoi (n-1, nspare, nto, nfrom); } int main (void) { Hanoi (6, 1, 2, 3); return 0; }


The least number of moves in tower of hanoi puzzle with 10 discs?

There is a formula for calculating the number of moves. The formula is 2^n-1. This means that to move one disk the number of moves can be calculated as 2^1-1. For two disks the calculation is 2^2-1. Using this formula the answer 1023 can be found


How long would it take to complete a Hanoi puzzle with 64 disks if you moved one disk per second?

It would take 264 - 1 seconds or, at one move per second, approx 585 billion years.


What is that game with the three poles and the different shaped discs and you have to move the discs from one pole to another?

It is called "Tower of Hanoi". See http://en.wikipedia.org/wiki/Tower_of_Hanoi for more info.