Tower of hanoi problem., Data Structure & Algorithms

Assignment Help:

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


Related Discussions:- Tower of hanoi problem.

Sorting, Retrieval of information is made simpler when it is stored into so...

Retrieval of information is made simpler when it is stored into some predefined order. Therefore, Sorting is a very important computer application activity. Several sorting algorit

Write an algorithm outputs number of books using psuedocode, A shop sells b...

A shop sells books, maps and magazines. Every item is identified by a unique 4 - digit code. All books have a code starting with a 1, all maps have a code which starts with a 2 and

Determine the algorithm for z-buffer method, Algorithm for Z-Buffer Method ...

Algorithm for Z-Buffer Method (a)  Initialize every pixel in the viewport to the smallest value of z, namely z0 the z-value of the rear clipping plane or "back-ground". Store a

Singly linked list , The two pointers per number of a doubly linked list pr...

The two pointers per number of a doubly linked list prepare programming quite easy. Singly linked lists as like the lean sisters of doubly linked lists. We need SItem to consider t

Red-black tree after insertion, The above 3 cases are also considered conve...

The above 3 cases are also considered conversely while the parent of Z is to the right of its own parent. All the different kind of cases can be illustrated through an instance. Le

Simulation of queues, Simulation of queues: Simulation is the process of f...

Simulation of queues: Simulation is the process of forming an abstract model of a real world situation in order to understand the effect of modifications and the effect of introdu

Calculus, basic calculation for algorith.

basic calculation for algorith.

Multidimensional array in one dimensional array, Q. By giving an example sh...

Q. By giving an example show how multidimensional array can be represented in one the dimensional array.

Cache simulator, how to design a cache simulator with 4-way set associative...

how to design a cache simulator with 4-way set associative cache

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd