Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem statement Towers of Hanoi is a simple puzzle that we're going to be using as practice for recursion and 2D arrays. The puzzle itself

image text in transcribed

Problem statement Towers of Hanoi is a simple puzzle that we're going to be using as practice for recursion and 2D arrays. The puzzle itself is very simple- it consists of three columns arranged from left to right, and some number of disks N of different sizes. To begin, the N disks are placed on the1 column in order of their size, with the largest disk at the bottom of the column. The puzzle's goal is to finish with the disks arranged in the same order (biggest on the bottom, smallest on the top) on the 2nd column. Of course, you can't just move the disks however you want! You need to follow these rules st You can only move one disk at a time by taking it off the top of its peg and putting it onto another peg You're not allowed to place a disk on top of another disk that's smaller-that is, every disk must be smaller than every disk beneath it on the peg . . (2 pts) Worksheet: Design First, begin by writing the steps on a piece of paper that represents the moves among the columns. For instance, with three disks, the smallest disk from the 1st post will be moved to the second peg, i.e. 1 -> 2. Then, the 2nd disk will be moved to the 3rd peg, i.e. 1->3, etc. Write the steps for the base case, n1 disks, n 2 disks, and n 3 disks. You should notice that you have 2n 1 moves for each of these cases. Also, note any pattern that you see, i.e when do you see the base case Here is an outline of the recursive towers function: void towers(int number_of_disks, int b[ ][3], int from_col, int to_col, int spare) If(number of disks is1) Call Towers with (number_of disks-1, b, from_ col, spare, to_ col) Move the disk Print the board Call Towers with (number_of disks-1, b, spare, to_col, from_ col) As a group with the TAs, walk through the algorithm provided for the towers) function with a board that has 1 disk and 3 columns, then 2 disks and 3 columns, and 3 disks with 3 columns e.g.. towers(1, 1, 2, 3);, towers(2, 1, 2, 3);, towers(3, 1, 2, 3);, etc. Provide the example walk through for the following calls towers(1, 1, 2, 3); towers(2, 1, 2, 3); towers (3, 1, 2, 3)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions