![]() To finish up our Tower, we use three more moves. Step 3 frees up the final peg so that we can place the largest disc on the bottom of the finisher peg where it belongs.The only move we can make is to put the tiny disc on top of the mid-sized disc on the helper peg. Now the largest disc is free and ready to be moved! However, since all the discs are taking up a peg, we cannot move the larger one until one of the smaller ones are out of the way.Since the smallest disc is on the destination peg, and a larger disc cannot be on top of a smaller disc, we will put the mid-sized disc on the helper peg.This frees up the middle disc so that we can begin to move it on its journey to the destination peg. The first move we will make is to move the smallest disc to the third “finisher” peg.Let’s talk through an example using three discs: This is where we will place certain disks when they need to be out of the way to make room for other disks to move without breaking the rules. This is where we want all of the pegs to end up. This is where all of the pegs currently reside. What does optimal moves look like? Let’s find out: All of the discs must remain on a peg except the one being usedīefore we solve this with recursive code in python, we will first try to understand the mathematical sequence of the Tower of Hanoi.įor example, a tower with 3 discs would take 7 steps to solve with optimal moves. You cannot place a larger disc on top of a smaller disc 3. You can only move one disc at a time and it has to be the top/only disc on that peg 2. Sounds easy enough, except there are three rules you have to follow: 1. Hopefully using the least amount of moves possible. The goal of the Tower of Hanoi is to move the discs to the designated peg. In the Tower of Hanoi, there are three pegs: two empty pegs and one with a stack of rings around it, each ring shrinking in size as you go up the peg. ![]() The Tower of Hanoi is a game and a classic example of how we can use recursion to solve problems. ![]() That’s recursion! If we are able to solve these smaller problems, it is likely we will be able to work our way up to solving the entire solution. When approaching a complex problem, one of the first things a programmer should do is to try to see if the problem can be solved by breaking it down into smaller versions of itself. There are many problems in computer science that are too big or too difficult to solve because they are too complex. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |