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
Document processing is quickly becoming one of the dominant functions of computers. Computers are utilized to edit, search & transport documents over the Internet, and to display d

memory address of any element of lower left triangular sparse matrix

Merge sort: Merge sort is a sorting algorithm that uses the idea of split and conquers. This algorithm splits the array into two halves, sorts them separately and then merges t

include int choice, stack[10], top, element; void menu(); void push(); void pop(); void showelements(); void main() { choice=element=1; top=0; menu()

How to create an General Tree and how to search general tree?

an electrical student designed a circuit in which the impedence in one part of a series circuit is 2+j8 ohms and the impedent is another part of the circuit is 4-j60 ohm mm program


write a pseudocode to input the top speed (in km''s/hours) of 5000 cars output the fastest speed and the slowest speed output the average (mean) speed of all the 5000 cars answers

how I can easily implement the bubble,selection,linear,binary searth algorithms?

Circular Queues:- A more efficient queue representation is get by regarding the array Q(1:n) as circular. It becomes more convenient to declare the array as Q(O: n-1), when  re