![]() I'll just state the algorithm here.You may need to know the stack data structure in C to implement it There are three poles.The source pole,The auxiliary pole and The Destination pole. hanoi.cpp : Defines the entry point for the console application. Yes ,the Tower of Hanoi problem can be solved using iteration in C. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. Ģ if can't Move N'th Block, must change Tower if current need move Block is order, we change Tower is Order else we change Tower is Retrorse ģ When changed Tower, must be restore Move Block rule Ĥ Adjust Rule 1: if Max Blocks Number is odd number, using Order->Retrorse The next problem, however, is to solve the Hanoi puzzle without recursion. The Wikipedia page on Tower of Hanoi has a section on a binary solution where the steps for an N-disk Tower of Hanoi are encoded in the binary representation of the numbers 0 to 2 N. For n 3, for instance, the solution is just: 1 to 3, 1 to 2, 3 to 2, 1 to 3, 2 to 1, 2 to 3, 1 to 3 where a to b indicates moving the top disk on peg a to peg b. Today, provide a method Iteration compute Hanoi Question ġ Move a Block to other Tower, Order->Retrorse ->Order ->Retrorse. It can be programmed without recursion and without stacks (or simulated stacks). ![]() ![]() The only problem size you can solve without this recursion is to move tower of size 1 to another place, because there can't be any disk on top and you can move in on top of any other disk. I want to write a program that solves the puzzle of the hanoi towers without using recursion. Tranlate N-1 Blocks from B to C, Using A So you need to solve the puzzle for size m-1 again (move tower of size m-1 from position 3 to 2). The i3 will ensure that at each move you will exclude a different stack (from the move): first you exclude stack B, then stack C, then stack A, then stack B. If you don't do the swap of B and C stacks, the algorithm will end up bringing the discs to B instead of C. Tranlate N-1 Blocks from A to B, Using C Brayoni, that's just how the Hanoi algorithm works. General we using Recurse Method process the Question Have 3 Tower A B C First A have 64 Blocks, these blocks have difference size Top->Down is small -> Large We must move these blocks from A to C, and when Moving we can use B ġ Large Blocks can't place on small block
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |