Question

Towers of Hanoi (15 points) Given: n disks, all of different sizes the size of the ith disk is į .3 pegs - A, B, C the number

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//TowerOfHanoi.java
class TowerOfHanoi
{
   public void tof(char src,char dest,char spare,int n)
   {
       //according to the strategy given
      
       if(n == 1)
       {
           System.out.println(n+" from "+src + " to "+ dest);
       }
       else
       {
           //shift n-1 disks from src to utility
          
           //shift the nth disk from source to dest
           //shift the n-1 disks from utility to dest
           tof(src, spare, dest, n-1);
           System.out.println(n + " from " + src + " to " + dest);
           tof(spare, dest, src, n-1);
       }
   }
   public static void main(String args[])
   {
       //check for number of arguments given
       if(args.length == 1)
       {
           try
           {
               //convert argument to int else print error message
               int n = Integer.parseInt(args[0]);
               //create tester object to test the program
               TowerOfHanoi tester = new TowerOfHanoi();
               //call tower of hanoi recursive function of n disks
               tester.tof('A', 'C', 'B', n);
           }
           catch(Exception e)
           {
               System.out.println("Given argument is not a number");
           }
       }
       else
       {
           System.out.println("Pass number of disks n as argument");
       }
      
   }
}
//TowerOfHanoi.java class TowerOfHanoi public void tof (char src,char dest,char spare,int n) //according to the strategy give 17 18 19 20 21 //shift the n-1 disks from utility to dest tof (src, spare, dest, n-1) Sy3tem . out .printIn(n + from + srcG4. C:Windowslsystem32cmd.exe java TowerOfHanoi 2 2 Pass number of disks n as argunent Uses fron A to B fron A to C java To

Add a comment
Know the answer?
Add Answer to:
Towers of Hanoi (15 points) Given: n disks, all of different sizes the size of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Describe a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary n (see...

    Describe a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary n (see Creativity Exercise C-5.16 for more details). C-5.16 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...

  • In the classic problem of the Towers of Hanoi, you have 3 rods and N disks...

    In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one).You have the following constraints: (A) Only one disk can be moved at a time. (B) A disk is slid off the top of one rod onto the next rod....

  • Please write a recursive Java program to solve the Tower of Hanoi game for n disks...

    Please write a recursive Java program to solve the Tower of Hanoi game for n disks on pole A. Please read the textbook page 176 – 180 to fully understand this game or puzzle. The game consists of n disks and three poles: A (the source), B (the destination), and C (the spare). Initially, all the disks are on pole A. The game is to move all disks (one by one) from pole A to pole B using pole C...

  • Write a recursive C++ program to solve the famous Towers of Hanoi problem. In addition to showing the moves, also display the current status of three pegs as follows.

    Write a recursive C++ program to solve the famous Towers ofHanoi problem. In addition to showing the moves, alsodisplay the current status of three pegs as follows.Initial pegsA54321BCMove disk 1 from peg A to peg BA5432B1C…

  • Part A [50] in c++ A toy that many children play with is a base with...

    Part A [50] in c++ A toy that many children play with is a base with three pegs and five disks of different diameters. The disks begin on one peg, with the largest disk on the bottom and the other four disks added on in order of size. The idea is to move the disks from the peg they are on to another peg by moving only one disk at a time and without ever putting a larger disk on...

  • A python algorithm written for Tower Of Hanoi. class Towers:     hasItMoved = []     toDest = []...

    A python algorithm written for Tower Of Hanoi. class Towers:     hasItMoved = []     toDest = []     def __init__(self):         self.current = 1         self.n = int(input("Enter the number of disks: "))         self.hasItMoved = [None] * 12         self.toDest = [1] * 12         for i in range(self.n):             self.toDest[i] = 0         self.r = self.n         self.hanoiStart(self.n, "Start", "Aux1", "Aux3", "Aux2", "Dest", self.current) # function to start     def hanoiStart(self, numOfDisks, start, source, dest, aux, last, current):         self.move(1, start, source, self.current)         self.current += 1         self.H1(self.n, "Start", "Aux1", "Aux3",...

  • Recursion Write a program to solve the Towers of Hanoi problem for a tower of size...

    Recursion Write a program to solve the Towers of Hanoi problem for a tower of size n, using both recursion and iteration. Time each method separately. Be very carefull to time only the actual work and avoid superfluous module calls and initialization, etc. Compare and contrast your two versions of the problem. Are they what you expected? Your analysis must contain a table of the times obtained for each run. For a tower of a particular size, your output should...

  • If possible, this tower of hanoi code is written in java. Could anyone make an attempt...

    If possible, this tower of hanoi code is written in java. Could anyone make an attempt to write it in python? package Hanoi;    import java.util.Scanner;    private int n,current,r;    public class Towers {    private Scanner in;    private int[] hasItMoved,toDest;    Towers(){    this.in=new Scanner(System.in);    this.current=1;    System.out.println("Enter the number of disks: ");    this.n = in.nextInt();    this.hasItMoved=new int[12];    this.toDest=new int[12];    for(int j=0;j<this.toDest.length;j++)    this.toDest[j]=1;    for(int i=1;i<=n;i++)    this.toDest[i]=0;    r=n;   ...

  • write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write...

    write in c programming language . question 5.36 Fibonaco for its rets ing terms, a) Write a warruse function fibonacci(n) that calculatus 0, 1, 1, 2, 3, 5, 8, 13, 21,... (n) that calculates the n" Fib begins with the terms and 1 and has the property preceding terms. a) Write a cand unsigned long long int for i number. Use unsigned int for the function's paramete her that can be printed on your system. type. b) Determine the largest...

  • language use : c++ Correct Tower application to move the disks from the pin on the...

    language use : c++ Correct Tower application to move the disks from the pin on the left to the pin on the right. Hint: draw the motion of th disks for a small set "say three" and walk your way through the recursion. #include <iostream> using namespace std; void towers(int,char,char,char); int main() int num; cout << "Number of disks: "; cin >> num; cout << "Sequences begin here \n"; towers(num, 'A', 'B! "C"); return 0; void towers(int num, char frompeg,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT