![]() What are the minimum moves to solve the Tower of Hanoi problem? Space Complexity: O(N), as the disks, take up the recursive stack space. Time Complexity: O(2^N) where N is the number of disks. TowerOfHanoi(n-1, aux_rod, to_rod, from_rod) Print("Move disk",n,"from rod",from_rod,"to rod",to_rod) TowerOfHanoi(n-1, from_rod, aux_rod, to_rod) Print("Move disk 1 from rod",from_rod,"to rod",to_rod) } Python Code for Recursive Approach def TowerOfHanoi(n, from_rod, to_rod, aux_rod): } Java Code for Recursive Approach static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) TowerOfHanoi(n - 1, aux_rod, to_rod, from_rod) TowerOfHanoi(n - 1, from_rod, aux_rod, to_rod) Ĭout << "Move disk " << n << " from rod " << from_rod << Repeat the above steps until it reaches the base case.Ĭ++ Code for Recursive Approach void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)Ĭout Similarly, again remove the top N – 1 disk from aux_rod to to_rod and recursively call the function on (N – 1, aux_rod, to_rod, from_rod).Now, since the top N – 1 disks have been removed from from_rod, move the last disk from from_rod to to_rod.from_rod to aux_rod by recursively calling the function on ( N – 1, from_rod, aux_rod, to_rod). Solve the problem recursively by moving disk 1, 2, 3,…, N – 1 i.e.Let us consider a recursive function that takes the following argument N, the number of disks, to_Rod, which indicates the rod which is moved to, from_rod, denoting the rod from which rod is removed, and aux_rod, denoting the rod which is used for transferring rods from from_rod to to_rod.The minimum number of moves to solve the Tower of Hanoi problem is 2^N – 1, where N is the number of disks. ![]() Similarly, the problem can be solved recursively for N = 3. Then the second disk is moved from rod 1 to rod 2 and finally, the first disk is moved again back to rod 2. ![]() So, one disk is moved from rod 1 to rod 3. Let us try to solve the problem for N = 2. The idea is to use a recursive approach to solve this problem. This should help you to implement the algorithm from the linked question.In 3 simple steps you can find your personalised career roadmap in Software development for FREE It now moves a disk from tower i to tower j I changed the declaration so that it makes sense to me. I don't know, how the function moveDisk() was intended to work. I added a method getHighestIdx() which returns the index before the first 0 value in the array So you don't have to pass the length of the array as a parameter to every method. Now each tower is its own array inside the towers array (makes more sense IMHO).Īrrays in Java know their size. I've changed your code a bit to make it more readable for me. Private static void moveDisk(inttowers, int num)ĭoes anyone have any suggestions on what I could do? moves the numbers in the array that represents disks Private static void displayTower (int towers, int num) Private static void createRings (inttowers, int num, int temp) How I decided to do it is by using 2D arrays and for the most part its working, my only problem is that I don't know how to move the disks while keeping it modular. I'm working on a Tower of Hanoi project for school which needs to ask the user how many disks there are and then it needs to create and then solve the tower with a visual included.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |