Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please proof this code to make this problem work (in Java) package TowersOfHanoi; import java.util.Scanner; public class TowersOfHanoi { private Scanner in; private int n,

Please proof this code to make this problem work (in Java)

image text in transcribed

package TowersOfHanoi; import java.util.Scanner; public class TowersOfHanoi { private Scanner in; private int n, current, r; private int[] hasItMoved, toDest; Towers() { this.in=new Scanner(System.in) this.current=1; System.out.pringtln("Enter the number of disks: "); /umber of disks this.current = in.nextInt(); this.hasItMoved=new int[12]; this.toDest = new int[12]; for(int j=0; j

public void TowersOfHanoi(int numOfDisks, String Start, String source, String dest, String aux, String last, int current) //recursive function to manipulate the disks { move(1, Start, source, this.current) this.current++; //call for move function H1(n, "Start", "Aux1", "Aux3", "Aux2", "Dest", this.current) //call for the recursive fuction move(1, dest, last, this.current) this.current++; //call for move function }

public int H1(int numOfDisks, String Start, String source, String dest, String aux, String last, int current) { if(numOfDisks == 1) //scenario if num of disks are equal to two { move(numOfDisks, source, aux, this.current) this.curreent++; move(numOfDisks, aux, dest, this.current) this.current++; } else if (numOfDisks == 2) //scenario if there are two disks { move(numOfDisks, source, aux, this.current) this.curreent++; move(numOfDisks, aux, dest, this.current) this.current++; if(this.current==4) { move(numOfDisks, Start, source, this.current) this.current++; } move(numOfDisks, source, aux, this.current) this.current++; move(numOfDisks-1, dest, aux, this.current) this.current++; move(numOfDisks-1, aux, source, thisi.current); this.current++; move(numOfDisks, aux, dest, this.current) this.current++; if(this.current==2) { move(2, dest, last, this.current) this.current++; } move(numOfDisks-1, source, aux, this.current); this.current++; move(numOfDisks-1, aux, dest, this.current) this.current++; } else if (numOfDisks > 2) //scenario if the number of disks are greater than 2 this.current = H1(numOfDisks-1, Start, source, dest, aux, last, this.current) if(this.hasItMoved[numOfDisks]!=1) { move(numOfDisks, Start, source, this.current) this.current++; this.hasItMoved[numOfDisks]=1; } move(numOfDisks, source, aux, this.current) this.current++; this.current = H1(numOfDisks-1, Start, dest, source, aux, last, this.current) move(numOfDisks, dest, last, this.current) this.current++; this.toDest[numOfDisks]=1; } if(numOfDisks==r) this.r--; this.current = H1(numOfDisks-1; Start, source, dest, aux, last, this.current); }; return ths.current; } //Printing out the moves of the Towers of Hanoi public void move(int aDisk, String source, String dest, int currentStep) { public void move(int aDisk, String source, String dest, int currentStep) { System.out.prinln("Move " + this.current +": Move disk " + aDisk + " from " + source + " to " + dest); } }

1. Solve the Towers of Hanoi game for the following graph G=(V.E) with Start, AU Aux2, Aux3, Aux4, Dest) and E= {(Start, Aux1)(Aux 1, Aux2), (Aux2, Aux 3), (Aux3, AUX4) (Aux4, Auxl), (Aux 1, Dest)}. (a) Design an algorithm and determine the time and space complexities of moving n disks from Start to Dest. (b) Implement this algorithm whereby your program prints out each of the moves of every disk. Show the output for n=1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. (If the output is too long, print out only the first 100 and the last 100 moves.)

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

Question

Use brainstorming effectively.

Answered: 1 week ago

Question

fscanf retums a special value EOF that stands for...

Answered: 1 week ago