Saturday, 6 July 2013

Tower of Hanoi using numbers

So, here I come up with a simple problem for you: Tower of Hanoi using numbers.

You have a predefined 1-D array, say A, of size n having numbers from 1 to n occupying positions 0 to n-1. Position 0 is the top of the array.
You have two other 2-D empty arrays, say B and C.
Now, what you have to do:
You have to empty the array A and transfer its elements to the array C such that at the end C has same configuration -> 1 to n, using some VALID STEPS.

Characteristics of a valid step is :
>Removing an number from the top of an array and placing it on the top of another array.
>You have to ensure that you are placing a smaller number on the top of a higher number (i.e. after each step each array should be in ascending order from top to bottom).
Here is a animation of the problem: http://upload.wikimedia.org/wikipedia/commons/6/60/Tower_of_Hanoi_4.gif
You must use only 3 arrays as described above.
Output :
The problem takes (2^n)-1 steps. You have print each step in the following format:
7 is moved from A to C.
6 is moved from B to A.
......
......

Thanks. 

No comments:

Post a Comment