In the case of 3 disks (small disk, medium disk, large disk):ġ) we move small disk from stack 0 to stack 1 but this is not the final stepĢ) we move the medium disk from stack 0 to stack 2ģ) we move small disk from stack 1 to stack 2 (this is not violation of the rule since the small disk is still above the medium disk).Ĥ) we move large disk from stack 0 to stack 1 (the final destination) make it the bottom of the stack 1 (just what we want it to be).ĥ) we move small disk from the top of stack 2 to the stack 0Ħ) we move the medium disk from stack 2 to stack 1 (final destination)ħ) and in the end we move the small disk from stack 0 to the top of stack 1 ( the end of the process)Īs for how the program did this kind of work, well since this is recursion it will take a while to explain step by step. So the case where there are two disk which stacked in stack 0 with smaller disk is above the larger disk:ġ) In order to meet the requirement that in the final destination stack (stack 1) the larger disk will be in the bottom and the smaller disk is above it we need to move the smaller disk first to auxiliary stack (stack 2)Ģ) Then we move the larger disk into the stack 1 which means we positioned the larger disk at the bottom of stack 1 right?ģ) lastly we move the smaller disk from stack 2 to stack 1 and we finished the process. Next You need to remember the stack numbering convention:Ġ: is the start stack which all disks are originally located at the beginning of the challengeġ : the final destination stack where all disks finally stacked accordinglyĢ: auxiliary stack to give spare room to maneuver the disks movements. So please remember that not just the disk can only moved one at a time but also the disks size is not uniform. The rules of the Tower of Hanoi challenge are:ġ) The monks can only move one disk at a timeĢ) The larger disk must be under the smaller disk Okay back to your question, I will answer it using real life logic. You can read more about Java recursion to solve the Towers of Hanoi here You can read more about the history of Towers of Hanoi here However, when there are 3 disks the first disk is moved from stack 0 to stack 1? * Since I am not sure how broad of explanation do you want I will focusing in answering on your question: * why does this program move the first disk from stack 0 to stack number 2 when 2 disks are present.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |