Tower of hanoi problem., Data Structure & Algorithms

Write an algorithm for getting solution to the Tower's of Hanoi problem. Explain the working of your algorithm (with 4 disks) with appropriate diagrams.

Ans:

void Hanoi(int n, char initial, char temp, char final)

{     if(n==1)

{ printf("move disk 1 from peg %c to peg%c\n",initial,final);

return;

}

Hanoi(n-1,initial, final, temp);

printf("move disk %d from peg %c to peg%c\n", n, initial, final);

Hanoi(n-1,temp,initial,final);

}

For n=4, let A, B and C denotes the initial, temp and final peg respectively. Using above algorithm, the  recursive  solution  for  n=4  disks  comprises  of  the  following 15 moves:

1549_Hanoi problem.png

Posted Date: 7/10/2012 3:08:47 AM | Location : United States







Related Discussions:- Tower of hanoi problem., Assignment Help, Ask Question on Tower of hanoi problem., Get Answer, Expert's Help, Tower of hanoi problem. Discussions

Write discussion on Tower of hanoi problem.
Your posts are moderated
Related Questions
Which data structure is required to change infix notation to postfix notation?    Stack function is used to change infix notation to postfix notatio n

The location of a node in a binary search tree is defined as a string such as LLRRL, which represents the node that you find by starting at the root, and traversing Left, traverse

Q. Prove the hypothesis that "A tree having 'm' nodes has exactly (m-1) branches".      Ans: A tree having m number of nodes has exactly (m-1) branches Proof: A root

Program: Creation of Doubly Linked List OUTPUT Input the values of the element -1111 to come out : 1 Input the values of the element -1111 to come out : 2 Inpu

Illustrate the Back Face Detection Method A single polyhedron is a convex solid, which has no external angle between faces less  than 180° and there is a simple object space me

Question 1 Write a program in 'C' to read N numbers and print them in descending order Question 2 Discuss the properties of ADT Question 3 Write a note on

Q. How can we free the memory by using Boundary tag method in the context of Dynamic memory management?

Definition of Algorithm Algorithm must have the following five characteristic features: 1.      Input 2.      Output 3.      Definiteness 4.      Effectiveness 5

What are the conditions under which sequential search of a list is preferred over binary search? Sequential Search is a preferred over binary search when the list is unordered

Explain Space Complexity Space Complexity :- The space complexity of an algorithm is the amount of memory it requires to run to completion. Some of the reasons to study space