In the Towers of Hanoi puzzle, we are given a platform with three pegs, a, b, and c, sticking out of it. On peg a is a stack of n disks, each larger than the next, so that the smallest is on the top and the largest is on the bottom. The puzzle is to move all the disks from peg a to peg c, moving one disk at a time, so that we never place a larger disk on top of a smaller one. See Figure 5.15 for an example of the case n =4. Describe a recursive
Figure 5.15: An illustration of the Towers of Hanoi puzzle.
Want to see the full answer?
Check out a sample textbook solutionChapter 5 Solutions
Data Structures and Algorithms in Java
Additional Engineering Textbook Solutions
Concepts Of Programming Languages
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Electric Circuits. (11th Edition)
Starting Out With Visual Basic (8th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
- You are given a stack of unknown number of cards each of which contains one number. Propose an algorithm for finding the average of the numbers on the cards. You may only look at one card at a time. How is this problem different from the previous one?arrow_forwardWrite a program using Python to solve the locker riddle which reads as follows: Imagine 100 lockers numbered 1 to 100 with 100 students lined up in front of those 100lockers:The first student opens every locker.The second student closes every 2nd locker.The 3rd student changes every 3rd locker; if it’s closed, she opens it; if it’s open, she closes it.The 4th student changes every fourth locker (e.g., 4th, 8th, etc.).The 5th student changes every 5th locker (e.g., 5th, 10th, etc.).That same pattern continues for all 100 students. Which lockers are left open after all 100 students have walked the rowof lockers?arrow_forwardIn chess, a walk for a particular piece is a sequence of legal moves for that piece, starting from a square of your choice, that visits every square of the board. A tour is a walk that visits every square only once. (See Figure 5.13.) 5.13 Prove by induction that there exists a knight’s walk of an n-by-n chessboard for any n ≥ 4. (It turns out that knight’s tours exist for all even n ≥ 6, but you don’t need to prove this fact.)arrow_forward
- Write a function find_equivalent_matchings that takes a matching and returns all matchings with the same pattern (including the original one). Using this function, draw all matchings equivalent to {(0, 7), (1, 4), (2, 3), (5, 6)}. You might find it helpful to generate all permutations of a list using itertools.permutation (imported above) as in the following code template. perms=permutations (list_of_items) for p in perms:arrow_forwardThere is a legend about a magical park with N × N trees. The trees are positioned in a square grid with Nrows (numbered from 1 to N from north to south) and N columns (numbered from 1 to N from west to east).The height (in metres) of each tree is an integer between 1 and N × N, inclusive. Magically, the height ofall trees is unique.Bunga is standing on the northmost point of the park and wants to count the number of visible trees for eachcolumn. Similarly, Lestari is standing on the westmost point of the park and wants to count the number ofvisible trees for each row. A tree X is visible if all other trees in front of the tree X are shorter than the treeX.For example, let N = 3 and the height (in metres) of the trees are as follows.6 1 87 5 32 9 4• On the first column, Bunga can see two trees, as the tree on the third row is obstructed by the othertrees.• On the second column, Bunga can see all three trees.• On the third column, Bunga can see only the tree on the first row, as the…arrow_forwardComputer Science Please do a simple JAVA code for the Monty Hall problem. Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to switch your choice? Please have the following Main allows the person playing to choose any door class door: should have data member methods Class person: should have data methodsarrow_forward
- 1. The entrance room (or the starting of the maze) is considered as level 1. Now, answer these following questions: (a). Write an algorithm to figure out how many maximum levels the maze can go up to. (b). Figure out the complexity of your algorithm. To create a maze some rooms of a building is connected. Starting room is called Entrance room. From the entrance room other rooms are there connected from it. However, some rooms of that maze- building have connected room from it, and some rooms do not have any connected room. Each of the room can have at most or up to two rooms connected from it. The starting room is the entrance room of the maze or building. Fore example: It can be any one like the followings: Exemple -: Room1 Roono Room Entrance Room Raom Room2 Room? Roo Roomo Here, maxinum level =7 Example -2; Entrace Room D- Room5 Room 2 Room 4 Maxximum level=3arrow_forwardWrite an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that none of them share the same row, column, or diagonal. In this case, "diagonal" means all diagonals, not just the two that bisect the board.arrow_forwardThere are m towns in a straight line, with a road joining each pair of consecutive towns. Legends say that an ordinary person in one of these towns will become a hero by completing a sequence of n quests. The first quest will be completed in their home town, but after each quest they may complete their next quest either in the same town or after moving to a neighbouring town.For example, if n = 5 and m = 4, a resident of town 2 may become a hero as follows:• begin in town 2 for quest 1,• then move to town 3 for quest 2,• stay in town 3 for quest 3,• return to town 2 for quest 4, and• move to town 1 for quest 5.Design an algorithm which runs in O(nm) time and finds the total number of waysto complete n quests.arrow_forward
- The Monty Hall game is a statistical problem: there is a TV show (like the Monty Hall show) that allows contestants to choose between three doors, A, B, and C. Behind one of these doors is a new car (the winning door), and behind the other two are goats (the losing doors). After the contestant makes a choice, the game show host shows a goat behind one of the doors NOT chosen. The contestant is then given a choice to either switch to the other, non-opened door, or stick with their original guess. The interesting part of this “game" is the statistics involved –a person has a 1/3 chance of originally picking a winning door. The other door – that which is not revealed to have a goat but also was not originally chosen – actually has a 2/3 chance of being a winning door. Therefore, it is in the contestant's best interest to switch doors. You will create a program that simulates the Monty Hall game, where the computer plays the role of the host. The program must have no outputs, but 1) Ask…arrow_forwardThe Monty Hall game is a statistical problem: there is a TV show (like the Monty Hall show) that allows contestants to choose between three doors, A, B, and C. Behind one of these doors is a new car (the winning door), and behind the other two are goats (the losing doors). After the contestant makes a choice, the game show host shows a goat behind one of the doors NOT chosen. The contestant is then given a choice to either switch to the other, non-opened door, or stick with their original guess. The interesting part of this “game" is the statistics involved-a person has a 1/3 chance of originally picking a winning door. The other door chosen – actually has a 2/3 chance of being a winning door. Therefore, it is in the contestant's best interest to switch doors. that which is not revealed to have a goat but also was not originally You will create a program that simulates the Monty Hall game, where the computer plays the role of the host. The program must have no outputs, but 1) Ask the…arrow_forwardThe Knapsack Problem is a famous computer science problem that is defined as follows: imagine you are carrying a knapsack with capacity to hold a total of weight C. You are selecting among n items with values A={a_1, a_2, ... , a_n} and associated weights W={w_1, w_2, ... , w_n}. Here the weights and values are all positive (but not necessarily unique). You wish to maximize the total value of the items you select not exceeding the given weight capacity, i.e. maximize sum_{a in A} such that sum_{w in W} <= C. Please note that you can only select your items once. a) We can reformulate this as a 2D bottom-up dynamic programming problem as follows. Define T_{i,j} as the highest possible value sum considering items 1 through i and total weight capacity j (j <= C). What is the base case i.e. T_{0,j} for all j and T_{i,0} for all i?, and What is the loop statement?arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning