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
Illustrate Trivariate Colour Models Conventional colour models based on the tristimulus theory all contain three variables and so are called trivariate models. Let us now consi

Example of Area Subdivision Method The procedure will be explained with respect to an illustrative problem, with the image consisting of five objects, namely a triangle (T), qu

Initially Nodes are inserted in an AVL tree in the same manner as an ordinary binary search tree. Though, the insertion algorithm for any AVL tree travels back along with the pa

Define about the class invariant A class invariant may not be true during execution of a public operation though it should be true between executions of public operations. For

What is an algorithm?  What are the characteristics of a good algorithm? An algorithm is "a step-by-step process for accomplishing some task'' An algorithm can be given in many

Let a be a well-formed formula. Let c be the number of binary logical operators in a. (Recall that ?, ?, ?, and ? are the binary logical operators). Let s be the number of proposit

The fundamental element of linked list is a "record" structure of at least two fields. The object which holds the data & refers to the next element into the list is called a node .

Given is the structure of an AVL tree: struct avl { struct node *left; int info; int bf; struct node *right; }; 2) A multiway tree of n order is an ord

Acyclic Graphs In a directed graph a path is said to form a cycle is there exists a path (A,B,C,.....P) such that A = P. A graph is called acyclic graph if there is no cycle in

In the earlier unit, we have discussed about the arrays. Arrays are data structures of fixed size. Insertion & deletion involves reshuffling of array elements. Thus, arraymanipulat